Mailcommon

tagwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "tagwidget.h"
8
9#include <KLineEditEventHandler>
10
11#include <KActionCollection>
12#include <KColorCombo>
13#include <KIconButton>
14#include <KKeySequenceWidget>
15#include <KLocalizedString>
16#include <QCheckBox>
17#include <QGridLayout>
18#include <QIcon>
19#include <QLabel>
20#include <QLineEdit>
21#include <QVBoxLayout>
22using namespace MailCommon;
23class MailCommon::TagWidgetPrivate
24{
25public:
26 TagWidgetPrivate() = default;
27
28 QLineEdit *mTagNameLineEdit = nullptr;
29
30 QCheckBox *mTextColorCheck = nullptr;
31 QCheckBox *mBackgroundColorCheck = nullptr;
32 QCheckBox *mTextFontCheck = nullptr;
33 QCheckBox *mInToolbarCheck = nullptr;
34
35 KColorCombo *mTextColorCombo = nullptr;
36 KColorCombo *mBackgroundColorCombo = nullptr;
37
38 QCheckBox *mBoldCheckBox = nullptr;
39 QCheckBox *mItalicCheckBox = nullptr;
40
41 KIconButton *mIconButton = nullptr;
42
43 KKeySequenceWidget *mKeySequenceWidget = nullptr;
44};
45
46TagWidget::TagWidget(const QList<KActionCollection *> &actionCollections, QWidget *parent)
47 : QWidget(parent)
48 , d(new MailCommon::TagWidgetPrivate)
49{
50 auto settings = new QGridLayout(this);
51 settings->setContentsMargins({});
52
53 // Stretcher layout for adding some space after the label
54 auto spacer = new QVBoxLayout();
55 settings->addLayout(spacer, 0, 0, 1, 2);
56
57 // First row for renaming
58 d->mTagNameLineEdit = new QLineEdit(this);
59 d->mTagNameLineEdit->setClearButtonEnabled(true);
60 KLineEditEventHandler::catchReturnKey(d->mTagNameLineEdit);
61 settings->addWidget(d->mTagNameLineEdit, 1, 1);
62
63 auto namelabel = new QLabel(i18nc("@label:listbox Name of the tag", "Name:"), this);
64 namelabel->setBuddy(d->mTagNameLineEdit);
65 settings->addWidget(namelabel, 1, 0);
66
67 connect(d->mTagNameLineEdit, &QLineEdit::textChanged, this, &TagWidget::slotEmitChangeCheck);
68
69 // Second row for text color
70 d->mTextColorCheck = new QCheckBox(i18nc("@option:check", "Change te&xt color:"), this);
71 settings->addWidget(d->mTextColorCheck, 2, 0);
72
73 d->mTextColorCombo = new KColorCombo(this);
74 settings->addWidget(d->mTextColorCombo, 2, 1);
75 d->mTextColorCombo->setEnabled(false);
76
77 connect(d->mTextColorCheck, &QCheckBox::toggled, d->mTextColorCombo, &KColorCombo::setEnabled);
78 connect(d->mTextColorCheck, &QCheckBox::checkStateChanged, this, &TagWidget::slotEmitChangeCheck);
79 connect(d->mTextColorCombo, &KColorCombo::activated, this, &TagWidget::slotEmitChangeCheck);
80
81 // Third row for text background color
82 d->mBackgroundColorCheck = new QCheckBox(i18nc("@option:check", "Change &background color:"), this);
83 settings->addWidget(d->mBackgroundColorCheck, 3, 0);
84
85 d->mBackgroundColorCombo = new KColorCombo(this);
86 settings->addWidget(d->mBackgroundColorCombo, 3, 1);
87 d->mBackgroundColorCombo->setEnabled(false);
88
89 connect(d->mBackgroundColorCheck, &QAbstractButton::toggled, d->mBackgroundColorCombo, &QWidget::setEnabled);
90 connect(d->mBackgroundColorCheck, &QCheckBox::checkStateChanged, this, &TagWidget::slotEmitChangeCheck);
91 connect(d->mBackgroundColorCombo, &KColorCombo::activated, this, &TagWidget::slotEmitChangeCheck);
92
93 // Fourth for font selection
94 d->mTextFontCheck = new QCheckBox(i18nc("@option:check", "Change fo&nt:"), this);
95 settings->addWidget(d->mTextFontCheck, 4, 0);
96
97 auto fontLayout = new QVBoxLayout;
98 settings->addLayout(fontLayout, 4, 1);
99
100 d->mBoldCheckBox = new QCheckBox(i18nc("@option:check", "&Bold"));
101 d->mBoldCheckBox->setEnabled(false);
102 fontLayout->addWidget(d->mBoldCheckBox);
103
104 d->mItalicCheckBox = new QCheckBox(i18nc("@option:check", "&Italics"));
105 d->mItalicCheckBox->setEnabled(false);
106 fontLayout->addWidget(d->mItalicCheckBox);
107
108 connect(d->mTextFontCheck, &QAbstractButton::toggled, d->mBoldCheckBox, &QWidget::setEnabled);
109 connect(d->mTextFontCheck, &QAbstractButton::toggled, d->mItalicCheckBox, &QWidget::setEnabled);
110 connect(d->mTextFontCheck, &QCheckBox::checkStateChanged, this, &TagWidget::slotEmitChangeCheck);
111 connect(d->mBoldCheckBox, &QAbstractButton::toggled, this, &TagWidget::slotEmitChangeCheck);
112 connect(d->mItalicCheckBox, &QAbstractButton::toggled, this, &TagWidget::slotEmitChangeCheck);
113
114 // Fifth for toolbar icon
115 d->mIconButton = new KIconButton(this);
116 d->mIconButton->setIconSize(16);
117 d->mIconButton->setIconType(KIconLoader::NoGroup, KIconLoader::Action);
118 d->mIconButton->setIcon(QIcon::fromTheme(QStringLiteral("mail-tagged")));
119 settings->addWidget(d->mIconButton, 5, 1);
120 connect(d->mIconButton, &KIconButton::iconChanged, this, &TagWidget::iconNameChanged);
121
122 auto iconlabel = new QLabel(i18nc("@label:textbox", "Message tag &icon:"), this);
123 iconlabel->setBuddy(d->mIconButton);
124 settings->addWidget(iconlabel, 5, 0);
125
126 // We do not connect the checkbox to icon selector since icons are used in the
127 // menus as well
128 connect(d->mIconButton, &KIconButton::iconChanged, this, &TagWidget::slotEmitChangeCheck);
129
130 // Sixth for shortcut
131 d->mKeySequenceWidget = new KKeySequenceWidget(this);
132 settings->addWidget(d->mKeySequenceWidget, 6, 1);
133 auto sclabel = new QLabel(i18nc("@label:textbox", "Shortc&ut:"), this);
134 sclabel->setBuddy(d->mKeySequenceWidget);
135 settings->addWidget(sclabel, 6, 0);
136 if (!actionCollections.isEmpty()) {
137 d->mKeySequenceWidget->setCheckActionCollections(actionCollections);
138 connect(d->mKeySequenceWidget, &KKeySequenceWidget::keySequenceChanged, this, &TagWidget::slotEmitChangeCheck);
139 } else {
140 d->mKeySequenceWidget->setEnabled(false);
141 }
142
143 // Seventh for Toolbar checkbox
144 d->mInToolbarCheck = new QCheckBox(i18nc("@option:check", "Enable &toolbar button"), this);
145 settings->addWidget(d->mInToolbarCheck, 7, 0);
146 connect(d->mInToolbarCheck, &QCheckBox::checkStateChanged, this, &TagWidget::slotEmitChangeCheck);
147}
148
149TagWidget::~TagWidget() = default;
150
151void TagWidget::slotEmitChangeCheck()
152{
153 Q_EMIT changed();
154}
155
156void TagWidget::setTagTextColor(const QColor &color)
157{
158 d->mTextColorCheck->setEnabled(true);
159 if (color.isValid()) {
160 d->mTextColorCheck->setChecked(true);
161 d->mTextColorCombo->setColor(color);
162 } else {
163 d->mTextColorCheck->setChecked(false);
164 d->mTextColorCombo->setColor(Qt::white);
165 }
166 d->mTextColorCombo->setEnabled(d->mTextColorCheck->isChecked());
167}
168
169void TagWidget::setTagBackgroundColor(const QColor &color)
170{
171 d->mBackgroundColorCheck->setEnabled(true);
172 if (color.isValid()) {
173 d->mBackgroundColorCheck->setChecked(true);
174 d->mBackgroundColorCombo->setColor(color);
175 } else {
176 d->mBackgroundColorCheck->setChecked(false);
177 d->mBackgroundColorCombo->setColor(Qt::white);
178 }
179 d->mBackgroundColorCombo->setEnabled(d->mBackgroundColorCheck->isChecked());
180}
181
182void TagWidget::setTagTextFormat(bool isBold, bool isItalic)
183{
184 d->mTextFontCheck->setEnabled(true);
185 d->mTextFontCheck->setChecked(isBold || isItalic);
186 d->mBoldCheckBox->setChecked(isBold);
187 d->mItalicCheckBox->setChecked(isItalic);
188}
189
190void TagWidget::recordTagSettings(MailCommon::Tag::Ptr tag)
191{
192 tag->textColor = d->mTextColorCheck->isChecked() ? d->mTextColorCombo->color() : QColor();
193
194 tag->backgroundColor = d->mBackgroundColorCheck->isChecked() ? d->mBackgroundColorCombo->color() : QColor();
195
196 tag->isBold = d->mTextFontCheck->isChecked() ? d->mBoldCheckBox->isChecked() : false;
197 tag->isItalic = d->mTextFontCheck->isChecked() ? d->mItalicCheckBox->isChecked() : false;
198
199 tag->iconName = iconButton()->icon();
200 if (d->mKeySequenceWidget->isEnabled()) {
201 d->mKeySequenceWidget->applyStealShortcut();
202 tag->shortcut = QKeySequence(d->mKeySequenceWidget->keySequence());
203 }
204
205 tag->inToolbar = d->mInToolbarCheck->isChecked();
206}
207
208QLineEdit *TagWidget::tagNameLineEdit() const
209{
210 return d->mTagNameLineEdit;
211}
212
213QCheckBox *TagWidget::textColorCheck() const
214{
215 return d->mTextColorCheck;
216}
217
218QCheckBox *TagWidget::textFontCheck() const
219{
220 return d->mTextFontCheck;
221}
222
223QCheckBox *TagWidget::backgroundColorCheck() const
224{
225 return d->mBackgroundColorCheck;
226}
227
228QCheckBox *TagWidget::inToolBarCheck() const
229{
230 return d->mInToolbarCheck;
231}
232
233KColorCombo *TagWidget::textColorCombo() const
234{
235 return d->mTextColorCombo;
236}
237
238KColorCombo *TagWidget::backgroundColorCombo() const
239{
240 return d->mBackgroundColorCombo;
241}
242
243QCheckBox *TagWidget::textBoldCheck() const
244{
245 return d->mBoldCheckBox;
246}
247
248QCheckBox *TagWidget::textItalicCheck() const
249{
250 return d->mItalicCheckBox;
251}
252
253KIconButton *TagWidget::iconButton() const
254{
255 return d->mIconButton;
256}
257
258KKeySequenceWidget *TagWidget::keySequenceWidget() const
259{
260 return d->mKeySequenceWidget;
261}
262
263#include "moc_tagwidget.cpp"
void activated(const QColor &col)
void iconChanged(const QString &icon)
const QString & icon() const
void keySequenceChanged(const QKeySequence &seq)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
The filter dialog.
void toggled(bool checked)
void addLayout(QLayout *layout, int stretch)
bool isValid() const const
QIcon fromTheme(const QString &name)
void textChanged(const QString &text)
bool isEmpty() const const
Q_EMITQ_EMIT
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 13 2024 11:49:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.