File indexing completed on 2025-01-05 04:49:46
0001 /* 0002 SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>* 0003 0004 SPDX-License-Identifier: GPL-2.0-only 0005 */ 0006 0007 #include "noteedit.h" 0008 #include "createnoteplugin_debug.h" 0009 #include <IncidenceEditor/IncidenceDialog> 0010 #include <IncidenceEditor/IncidenceDialogFactory> 0011 #include <messageviewer/globalsettings_messageviewer.h> 0012 0013 #include <Akonadi/CollectionComboBox> 0014 0015 #include <KGuiItem> 0016 #include <KLocalizedString> 0017 #include <KStandardGuiItem> 0018 0019 #include <QEvent> 0020 #include <QHBoxLayout> 0021 #include <QIcon> 0022 #include <QKeyEvent> 0023 #include <QLabel> 0024 #include <QLineEdit> 0025 #include <QPushButton> 0026 0027 namespace MessageViewer 0028 { 0029 QAbstractItemModel *_k_noteEditStubModel = nullptr; 0030 } 0031 0032 using namespace MessageViewer; 0033 0034 NoteEdit::NoteEdit(QWidget *parent) 0035 : QWidget(parent) 0036 , mNoteEdit(new QLineEdit(this)) 0037 , mCollectionCombobox(new Akonadi::CollectionComboBox(_k_noteEditStubModel, this)) 0038 , mSaveButton(new QPushButton(QIcon::fromTheme(QStringLiteral("view-pim-notes")), i18n("&Save"), this)) 0039 { 0040 auto vbox = new QVBoxLayout(this); 0041 vbox->setContentsMargins(5, 5, 5, 5); 0042 vbox->setSpacing(2); 0043 0044 auto hbox = new QHBoxLayout; 0045 hbox->setContentsMargins({}); 0046 hbox->setSpacing(2); 0047 vbox->addLayout(hbox); 0048 0049 auto lab = new QLabel(i18n("Note:"), this); 0050 hbox->addWidget(lab); 0051 0052 mNoteEdit->setClearButtonEnabled(true); 0053 mNoteEdit->setObjectName(QLatin1StringView("noteedit")); 0054 mNoteEdit->setFocus(); 0055 connect(mNoteEdit, &QLineEdit::returnPressed, this, &NoteEdit::slotReturnPressed); 0056 connect(mNoteEdit, &QLineEdit::textChanged, this, &NoteEdit::slotUpdateButtons); 0057 hbox->addWidget(mNoteEdit); 0058 0059 hbox->addSpacing(5); 0060 0061 mCollectionCombobox->setAccessRightsFilter(Akonadi::Collection::CanCreateItem); 0062 mCollectionCombobox->setMinimumWidth(250); 0063 mCollectionCombobox->setMimeTypeFilter(QStringList() << Akonadi::NoteUtils::noteMimeType()); 0064 mCollectionCombobox->setObjectName(QLatin1StringView("akonadicombobox")); 0065 #ifndef QT_NO_ACCESSIBILITY 0066 mCollectionCombobox->setAccessibleDescription(i18n("Calendar where the new event will be stored.")); 0067 #endif 0068 mCollectionCombobox->setToolTip(i18n("Calendar where the new event will be stored")); 0069 connect(mCollectionCombobox, &Akonadi::CollectionComboBox::currentIndexChanged, this, &NoteEdit::slotCollectionChanged); 0070 connect(mCollectionCombobox, &Akonadi::CollectionComboBox::activated, this, &NoteEdit::slotCollectionChanged); 0071 hbox->addWidget(mCollectionCombobox); 0072 0073 hbox = new QHBoxLayout; 0074 hbox->setContentsMargins({}); 0075 hbox->setSpacing(2); 0076 vbox->addLayout(hbox); 0077 0078 hbox->addStretch(1); 0079 0080 hbox = new QHBoxLayout; 0081 hbox->setSpacing(2); 0082 hbox->setContentsMargins({}); 0083 vbox->addLayout(hbox); 0084 0085 hbox->addStretch(1); 0086 0087 mSaveButton->setObjectName(QLatin1StringView("save-button")); 0088 mSaveButton->setEnabled(false); 0089 #ifndef QT_NO_ACCESSIBILITY 0090 mSaveButton->setAccessibleDescription(i18n("Create new note and close this widget.")); 0091 #endif 0092 connect(mSaveButton, &QPushButton::clicked, this, &NoteEdit::slotReturnPressed); 0093 hbox->addWidget(mSaveButton); 0094 0095 auto btn = new QPushButton(this); 0096 KGuiItem::assign(btn, KStandardGuiItem::cancel()); 0097 btn->setObjectName(QLatin1StringView("close-button")); 0098 #ifndef QT_NO_ACCESSIBILITY 0099 btn->setAccessibleDescription(i18n("Close the widget for creating new notes.")); 0100 #endif 0101 connect(btn, &QPushButton::clicked, this, &NoteEdit::slotCloseWidget); 0102 hbox->addWidget(btn); 0103 0104 readConfig(); 0105 setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed)); 0106 installEventFilter(this); 0107 mCollectionCombobox->installEventFilter(this); 0108 } 0109 0110 NoteEdit::~NoteEdit() 0111 { 0112 writeConfig(); 0113 } 0114 0115 void NoteEdit::writeConfig() 0116 { 0117 if (mCollectionCombobox->currentCollection().id() != MessageViewer::MessageViewerSettingsBase::self()->lastNoteSelectedFolder()) { 0118 MessageViewer::MessageViewerSettingsBase::self()->setLastNoteSelectedFolder(mCollectionCombobox->currentCollection().id()); 0119 MessageViewer::MessageViewerSettingsBase::self()->save(); 0120 } 0121 } 0122 0123 void NoteEdit::slotUpdateButtons(const QString &subject) 0124 { 0125 const bool subjectIsNotEmpty = !subject.trimmed().isEmpty(); 0126 mSaveButton->setEnabled(subjectIsNotEmpty); 0127 } 0128 0129 void NoteEdit::showNoteEdit() 0130 { 0131 mNoteEdit->setFocus(); 0132 show(); 0133 } 0134 0135 void NoteEdit::readConfig() 0136 { 0137 const qint64 id = MessageViewer::MessageViewerSettingsBase::self()->lastNoteSelectedFolder(); 0138 if (id != -1) { 0139 mCollectionCombobox->setDefaultCollection(Akonadi::Collection(id)); 0140 } 0141 } 0142 0143 Akonadi::Collection NoteEdit::collection() const 0144 { 0145 return mCollection; 0146 } 0147 0148 void NoteEdit::slotCollectionChanged(int /*index*/) 0149 { 0150 setCollection(mCollectionCombobox->currentCollection()); 0151 } 0152 0153 void NoteEdit::setCollection(const Akonadi::Collection &value) 0154 { 0155 if (mCollection != value) { 0156 mCollection = value; 0157 Q_EMIT collectionChanged(mCollection); 0158 } 0159 } 0160 0161 KMime::Message::Ptr NoteEdit::message() const 0162 { 0163 return mMessage; 0164 } 0165 0166 void NoteEdit::setMessage(const KMime::Message::Ptr &value) 0167 { 0168 if (mMessage != value) { 0169 mMessage = value; 0170 const KMime::Headers::Subject *const subject = mMessage ? mMessage->subject(false) : nullptr; 0171 if (subject) { 0172 mNoteEdit->setText(subject->asUnicodeString()); 0173 mNoteEdit->selectAll(); 0174 mNoteEdit->setFocus(); 0175 } else { 0176 mNoteEdit->clear(); 0177 } 0178 Q_EMIT messageChanged(mMessage); 0179 } 0180 } 0181 0182 void NoteEdit::slotCloseWidget() 0183 { 0184 writeConfig(); 0185 mNoteEdit->clear(); 0186 mMessage = KMime::Message::Ptr(); 0187 hide(); 0188 } 0189 0190 void NoteEdit::slotReturnPressed() 0191 { 0192 if (!mMessage) { 0193 qCDebug(CREATENOTEPLUGIN_LOG) << " Message is null"; 0194 return; 0195 } 0196 const Akonadi::Collection collection = mCollectionCombobox->currentCollection(); 0197 if (!collection.isValid()) { 0198 qCDebug(CREATENOTEPLUGIN_LOG) << " Collection is not valid"; 0199 return; 0200 } 0201 0202 if (!mNoteEdit->text().trimmed().isEmpty()) { 0203 Akonadi::NoteUtils::NoteMessageWrapper note; 0204 note.setTitle(mNoteEdit->text()); 0205 Q_EMIT createNote(note.message(), collection); 0206 mNoteEdit->clear(); 0207 hide(); 0208 } 0209 } 0210 0211 bool NoteEdit::eventFilter(QObject *object, QEvent *e) 0212 { 0213 // Close the bar when pressing Escape. 0214 // Not using a QShortcut for this because it could conflict with 0215 // window-global actions (e.g. Emil Sedgh binds Esc to "close tab"). 0216 // With a shortcut override we can catch this before it gets to kactions. 0217 const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride); 0218 if (shortCutOverride) { 0219 auto kev = static_cast<QKeyEvent *>(e); 0220 if (kev->key() == Qt::Key_Escape) { 0221 e->accept(); 0222 slotCloseWidget(); 0223 return true; 0224 } else if (kev->key() == Qt::Key_Enter || kev->key() == Qt::Key_Return || kev->key() == Qt::Key_Space) { 0225 e->accept(); 0226 if (object == mCollectionCombobox) { 0227 mCollectionCombobox->showPopup(); 0228 } 0229 return true; 0230 } 0231 } 0232 return QWidget::eventFilter(object, e); 0233 } 0234 0235 #include "moc_noteedit.cpp"