File indexing completed on 2024-05-12 05:13:15

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0005  */
0006 
0007 #include "noteeditdialog.h"
0008 
0009 #include <Akonadi/CollectionComboBox>
0010 #include <Akonadi/NoteUtils>
0011 
0012 #include <TextCustomEditor/RichTextEditor>
0013 #include <TextCustomEditor/RichTextEditorWidget>
0014 
0015 #include <KConfigGroup>
0016 #include <KLocalizedString>
0017 #include <KSharedConfig>
0018 
0019 #include <KWindowConfig>
0020 #include <QDialogButtonBox>
0021 #include <QLabel>
0022 #include <QLineEdit>
0023 #include <QPushButton>
0024 #include <QVBoxLayout>
0025 #include <QWindow>
0026 
0027 using namespace CalendarSupport;
0028 
0029 QAbstractItemModel *NoteEditDialog::_k_noteEditStubModel = nullptr;
0030 
0031 namespace
0032 {
0033 static const char myNoteEditDialogGroupName[] = "NoteEditDialog";
0034 }
0035 
0036 NoteEditDialog::NoteEditDialog(QWidget *parent)
0037     : QDialog(parent)
0038     , mNoteTitle(new QLineEdit(this))
0039 {
0040     setAttribute(Qt::WA_DeleteOnClose);
0041     setWindowTitle(i18nc("@title:window", "Create Note"));
0042     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0043     auto mainWidget = new QWidget(this);
0044     auto mainLayout = new QVBoxLayout(this);
0045     mainLayout->addWidget(mainWidget);
0046     connect(buttonBox, &QDialogButtonBox::accepted, this, &NoteEditDialog::accept);
0047     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0048     mainLayout->addWidget(buttonBox);
0049 
0050     buttonBox->button(QDialogButtonBox::Cancel)->setText(i18nc("@action:button", "Cancel"));
0051 
0052     mOkButton = buttonBox->button(QDialogButtonBox::Ok);
0053     mOkButton->setObjectName(QLatin1StringView("save-button"));
0054     mOkButton->setDefault(true);
0055     mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0056     mOkButton->setText(i18nc("@action:button", "Save"));
0057     mOkButton->setIcon(QIcon::fromTheme(QStringLiteral("view-pim-notes")));
0058     mOkButton->setEnabled(false);
0059 
0060     auto layout = new QGridLayout(mainWidget);
0061     layout->setContentsMargins(0, 0, 0, 0);
0062     auto hbox = new QHBoxLayout;
0063     hbox->setContentsMargins(0, 0, 0, 0);
0064     hbox->setSpacing(2);
0065 
0066     mNoteTitle->setClearButtonEnabled(true);
0067     mNoteTitle->setObjectName(QLatin1StringView("notetitle"));
0068     mNoteTitle->setFocus();
0069     connect(mNoteTitle, &QLineEdit::textChanged, this, &NoteEditDialog::slotUpdateButtons);
0070 
0071     mCollectionCombobox = new Akonadi::CollectionComboBox(_k_noteEditStubModel, this);
0072     mCollectionCombobox->setAccessRightsFilter(Akonadi::Collection::CanCreateItem);
0073     mCollectionCombobox->setMinimumWidth(250);
0074     mCollectionCombobox->setMimeTypeFilter(QStringList() << Akonadi::NoteUtils::noteMimeType());
0075     mCollectionCombobox->setObjectName(QLatin1StringView("akonadicombobox"));
0076 #ifndef QT_NO_ACCESSIBILITY
0077     mCollectionCombobox->setAccessibleDescription(i18nc("@info", "Calendar where the new note will be stored."));
0078 #endif
0079     mCollectionCombobox->setToolTip(i18nc("@info:tooltip", "Calendar where the new note will be stored."));
0080     connect(mCollectionCombobox, &Akonadi::CollectionComboBox::currentIndexChanged, this, &NoteEditDialog::slotCollectionChanged);
0081     connect(mCollectionCombobox, &Akonadi::CollectionComboBox::activated, this, &NoteEditDialog::slotCollectionChanged);
0082 
0083     mNoteText = new TextCustomEditor::RichTextEditorWidget(parent);
0084     mNoteText->setObjectName(QLatin1StringView("notetext"));
0085     connect(mNoteText->editor(), &TextCustomEditor::RichTextEditor::textChanged, this, &NoteEditDialog::slotUpdateButtons);
0086 
0087     // First line
0088     hbox->addWidget(mNoteTitle);
0089     hbox->addSpacing(5);
0090     hbox->addWidget(mCollectionCombobox);
0091 
0092     auto lab = new QLabel(i18nc("@label specify the title for this note", "Title:"), this);
0093     layout->addWidget(lab, 0, 0);
0094     layout->addLayout(hbox, 0, 1);
0095 
0096     // Second Line
0097     lab = new QLabel(i18nc("@label specify the text for this note", "Text:"), this);
0098     layout->addWidget(lab, 1, 0);
0099     layout->setAlignment(lab, Qt::AlignTop);
0100     layout->addWidget(mNoteText, 1, 1);
0101 
0102     readConfig();
0103 }
0104 
0105 NoteEditDialog::~NoteEditDialog()
0106 {
0107     disconnect(mNoteText->editor(), &TextCustomEditor::RichTextEditor::textChanged, this, &NoteEditDialog::slotUpdateButtons);
0108     writeConfig();
0109 }
0110 
0111 void NoteEditDialog::readConfig()
0112 {
0113     create(); // ensure a window is created
0114     windowHandle()->resize(QSize(500, 300));
0115     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myNoteEditDialogGroupName));
0116     KWindowConfig::restoreWindowSize(windowHandle(), group);
0117     resize(windowHandle()->size()); // workaround for QTBUG-40584
0118 }
0119 
0120 void NoteEditDialog::writeConfig()
0121 {
0122     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myNoteEditDialogGroupName));
0123     KWindowConfig::saveWindowSize(windowHandle(), group);
0124     group.sync();
0125 }
0126 
0127 void NoteEditDialog::slotUpdateButtons()
0128 {
0129     if (mNoteTitle->text().trimmed().isEmpty() && mNoteText->isEmpty()) {
0130         mOkButton->setEnabled(false);
0131     } else {
0132         mOkButton->setEnabled(true);
0133     }
0134 }
0135 
0136 Akonadi::Collection NoteEditDialog::collection() const
0137 {
0138     return mCollection;
0139 }
0140 
0141 void NoteEditDialog::slotCollectionChanged(int index)
0142 {
0143     Q_UNUSED(index)
0144     setCollection(mCollectionCombobox->currentCollection());
0145 }
0146 
0147 void NoteEditDialog::setCollection(const Akonadi::Collection &value)
0148 {
0149     if (mCollection != value) {
0150         mCollection = value;
0151         Q_EMIT collectionChanged(mCollection);
0152     }
0153 }
0154 
0155 void NoteEditDialog::accept()
0156 {
0157     QDialog::accept();
0158     const Akonadi::Collection collection = mCollectionCombobox->currentCollection();
0159     if (!collection.isValid()) {
0160         return;
0161     }
0162 
0163     if (mNoteTitle->text().isEmpty() && mNoteText->isEmpty()) {
0164         return;
0165     }
0166 
0167     Akonadi::NoteUtils::NoteMessageWrapper note(mItem.payload<KMime::Message::Ptr>());
0168     note.setTitle(mNoteTitle->text());
0169     if (mNoteText->acceptRichText()) {
0170         note.setText(mNoteText->editor()->toHtml(), Qt::RichText);
0171     } else {
0172         note.setText(mNoteText->editor()->toPlainText(), Qt::PlainText);
0173     }
0174     mItem.setPayload<KMime::Message::Ptr>(note.message());
0175     Q_EMIT createNote(mItem, collection);
0176 }
0177 
0178 void NoteEditDialog::load(const Akonadi::Item &item)
0179 {
0180     mItem = item;
0181     Akonadi::NoteUtils::NoteMessageWrapper note(item.payload<KMime::Message::Ptr>());
0182     mNoteText->editor()->setHtml(note.text());
0183     if (note.textFormat() == Qt::RichText) {
0184         mNoteText->setAcceptRichText(true);
0185     } else {
0186         mNoteText->setAcceptRichText(false);
0187     }
0188     mNoteTitle->setText(note.title());
0189 }
0190 
0191 KMime::Message::Ptr NoteEditDialog::note() const
0192 {
0193     if (mItem.hasPayload<KMime::Message::Ptr>()) {
0194         return mItem.payload<KMime::Message::Ptr>();
0195     } else {
0196         return {};
0197     }
0198 }
0199 
0200 #include "moc_noteeditdialog.cpp"