| 1 |
/** =========================================================== |
| 2 |
* @file |
| 3 |
* |
| 4 |
* This file is a part of digiKam project |
| 5 |
* <a href="https://www.digikam.org">https://www.digikam.org</a> |
| 6 |
* |
| 7 |
* @date 2008-08-16 |
| 8 |
* @brief a combo box widget re-implemented with a |
| 9 |
* reset button to switch to a default item |
| 10 |
* |
| 11 |
* @author Copyright (C) 2008-2015 by Gilles Caulier |
| 12 |
* <a href="mailto:caulier dot gilles at gmail dot com">caulier dot gilles at gmail dot com</a> |
| 13 |
* |
| 14 |
* This program is free software; you can redistribute it |
| 15 |
* and/or modify it under the terms of the GNU General |
| 16 |
* Public License as published by the Free Software Foundation; |
| 17 |
* either version 2, or (at your option) |
| 18 |
* any later version. |
| 19 |
* |
| 20 |
* This program is distributed in the hope that it will be useful, |
| 21 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
* GNU General Public License for more details. |
| 24 |
* |
| 25 |
* ============================================================ */ |
| 26 |
|
| 27 |
#include "rcombobox.h" |
| 28 |
|
| 29 |
// Qt includes |
| 30 |
|
| 31 |
#include <QApplication> |
| 32 |
#include <QStyle> |
| 33 |
#include <QToolButton> |
| 34 |
#include <QHBoxLayout> |
| 35 |
|
| 36 |
// KDE includes |
| 37 |
|
| 38 |
#include <klocalizedstring.h> |
| 39 |
|
| 40 |
#include <kis_icon_utils.h> |
| 41 |
|
| 42 |
namespace KDcrawIface |
| 43 |
{ |
| 44 |
|
| 45 |
class Q_DECL_HIDDEN RComboBox::Private |
| 46 |
{ |
| 47 |
|
| 48 |
public: |
| 49 |
|
| 50 |
Private() |
| 51 |
{ |
| 52 |
defaultIndex = 0; |
| 53 |
resetButton = 0; |
| 54 |
combo = 0; |
| 55 |
} |
| 56 |
|
| 57 |
int defaultIndex; |
| 58 |
|
| 59 |
QToolButton* resetButton; |
| 60 |
|
| 61 |
QComboBox* combo; |
| 62 |
}; |
| 63 |
|
| 64 |
RComboBox::RComboBox(QWidget* const parent) |
| 65 |
: QWidget(parent), d(new Private) |
| 66 |
{ |
| 67 |
QHBoxLayout* const hlay = new QHBoxLayout(this); |
| 68 |
d->combo = new QComboBox(this); |
| 69 |
d->resetButton = new QToolButton(this); |
| 70 |
d->resetButton->setAutoRaise(true); |
| 71 |
d->resetButton->setFocusPolicy(Qt::NoFocus); |
| 72 |
d->resetButton->setIcon(KisIconUtils::loadIcon("document-revert").pixmap(16, 16)); |
| 73 |
d->resetButton->setToolTip(i18nc("@info:tooltip", "Reset to default value")); |
| 74 |
|
| 75 |
hlay->addWidget(d->combo); |
| 76 |
hlay->addWidget(d->resetButton); |
| 77 |
hlay->setStretchFactor(d->combo, 10); |
| 78 |
hlay->setContentsMargins(0, 0, 0, 0); |
| 79 |
hlay->setSpacing(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing)); |
| 80 |
|
| 81 |
// ------------------------------------------------------------- |
| 82 |
|
| 83 |
connect(d->resetButton, &QToolButton::clicked, |
| 84 |
this, &RComboBox::slotReset); |
| 85 |
|
| 86 |
connect(d->combo, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), |
| 87 |
this, &RComboBox::slotItemActivated); |
| 88 |
|
| 89 |
connect(d->combo, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), |
| 90 |
this, &RComboBox::slotCurrentIndexChanged); |
| 91 |
} |
| 92 |
|
| 93 |
RComboBox::~RComboBox() |
| 94 |
{ |
| 95 |
delete d; |
| 96 |
} |
| 97 |
|
| 98 |
QComboBox* RComboBox::combo() const |
| 99 |
{ |
| 100 |
return d->combo; |
| 101 |
} |
| 102 |
|
| 103 |
void RComboBox::addItem(const QString& t, int index) |
| 104 |
{ |
| 105 |
d->combo->addItem(t, index); |
| 106 |
} |
| 107 |
|
| 108 |
void RComboBox::insertItem(int index, const QString& t) |
| 109 |
{ |
| 110 |
d->combo->insertItem(index, t); |
| 111 |
} |
| 112 |
|
| 113 |
int RComboBox::currentIndex() const |
| 114 |
{ |
| 115 |
return d->combo->currentIndex(); |
| 116 |
} |
| 117 |
|
| 118 |
void RComboBox::setCurrentIndex(int v) |
| 119 |
{ |
| 120 |
d->combo->setCurrentIndex(v); |
| 121 |
} |
| 122 |
|
| 123 |
int RComboBox::defaultIndex() const |
| 124 |
{ |
| 125 |
return d->defaultIndex; |
| 126 |
} |
| 127 |
|
| 128 |
void RComboBox::setDefaultIndex(int v) |
| 129 |
{ |
| 130 |
d->defaultIndex = v; |
| 131 |
d->combo->setCurrentIndex(d->defaultIndex); |
| 132 |
slotItemActivated(v); |
| 133 |
} |
| 134 |
|
| 135 |
void RComboBox::slotReset() |
| 136 |
{ |
| 137 |
d->combo->setCurrentIndex(d->defaultIndex); |
| 138 |
d->resetButton->setEnabled(false); |
| 139 |
slotItemActivated(d->defaultIndex); |
| 140 |
Q_EMIT reset(); |
| 141 |
} |
| 142 |
|
| 143 |
void RComboBox::slotItemActivated(int v) |
| 144 |
{ |
| 145 |
d->resetButton->setEnabled(v != d->defaultIndex); |
| 146 |
Q_EMIT activated(v); |
| 147 |
} |
| 148 |
|
| 149 |
void RComboBox::slotCurrentIndexChanged(int v) |
| 150 |
{ |
| 151 |
d->resetButton->setEnabled(v != d->defaultIndex); |
| 152 |
Q_EMIT currentIndexChanged(v); |
| 153 |
} |
| 154 |
|
| 155 |
} // namespace KDcrawIface |