| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2014-05-17 |
| 7 |
* Description : Album Labels Tree View - Private containers and classes. |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2014-2015 by Mohamed_Anwer <m_dot_anwer at gmx dot com> |
| 10 |
* SPDX-FileCopyrightText: 2014-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 11 |
* |
| 12 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 13 |
* |
| 14 |
* ============================================================ */ |
| 15 |
|
| 16 |
#include "labelstreeview_p.h" |
| 17 |
|
| 18 |
namespace Digikam |
| 19 |
{ |
| 20 |
|
| 21 |
ColorLabelValidator::ColorLabelValidator(int maxLength, QObject* const parent) |
| 22 |
: QValidator (parent), |
| 23 |
m_maxLength(maxLength) |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
QValidator::State ColorLabelValidator::validate(QString& input, int& /*pos*/) const |
| 28 |
{ |
| 29 |
if (input.length() <= m_maxLength) |
| 30 |
{ |
| 31 |
return QValidator::Acceptable; |
| 32 |
} |
| 33 |
|
| 34 |
return QValidator::Invalid; |
| 35 |
} |
| 36 |
|
| 37 |
// --- |
| 38 |
|
| 39 |
ColorLabelDelegate::ColorLabelDelegate(int maxLength, QObject* const parent) |
| 40 |
: QStyledItemDelegate(parent), |
| 41 |
m_maxLength (maxLength) |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
QWidget* ColorLabelDelegate::createEditor(QWidget* parent, |
| 46 |
const QStyleOptionViewItem& /*option*/, |
| 47 |
const QModelIndex& /*index*/) const |
| 48 |
{ |
| 49 |
QLineEdit* const editor = new QLineEdit(parent); |
| 50 |
editor->setValidator(new ColorLabelValidator(m_maxLength, editor)); |
| 51 |
editor->setClearButtonEnabled(true); |
| 52 |
editor->setPlaceholderText(i18n("Customize label name here...")); |
| 53 |
|
| 54 |
return editor; |
| 55 |
} |
| 56 |
|
| 57 |
void ColorLabelDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const |
| 58 |
{ |
| 59 |
QString text = index.model()->data(index, Qt::EditRole).toString(); |
| 60 |
QLineEdit* const lineEdit = qobject_cast<QLineEdit*>(editor); |
| 61 |
lineEdit->setText(text); |
| 62 |
} |
| 63 |
|
| 64 |
void ColorLabelDelegate::setModelData(QWidget* editor, |
| 65 |
QAbstractItemModel* model, |
| 66 |
const QModelIndex& index) const |
| 67 |
{ |
| 68 |
QLineEdit* const lineEdit = qobject_cast<QLineEdit*>(editor); |
| 69 |
QString text = lineEdit->text(); |
| 70 |
|
| 71 |
if (text.isEmpty()) |
| 72 |
{ |
| 73 |
// If empty, reset to default name. |
| 74 |
|
| 75 |
int label = index.model()->data(index, Qt::UserRole).toInt(); |
| 76 |
text = ColorLabelWidget::labelColorName((ColorLabel)label); |
| 77 |
} |
| 78 |
|
| 79 |
model->setData(index, text, Qt::EditRole); |
| 80 |
} |
| 81 |
|
| 82 |
// --- |
| 83 |
|
| 84 |
LabelsTreeView::Private::Private(QTreeWidget* const w) |
| 85 |
: itemIterator(w) |
| 86 |
{ |
| 87 |
} |
| 88 |
|
| 89 |
} // namespace Digikam |