File indexing completed on 2025-01-19 04:51:22

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "knoteprintselectednotesdialog.h"
0008 #include "knoteprintobject.h"
0009 #include "notes/knote.h"
0010 #include "print/knoteprintselectthemecombobox.h"
0011 
0012 #include <KConfigGroup>
0013 #include <KLocalizedString>
0014 #include <QIcon>
0015 
0016 #include <KSharedConfig>
0017 #include <KWindowConfig>
0018 #include <QDialogButtonBox>
0019 #include <QHBoxLayout>
0020 #include <QLabel>
0021 #include <QListWidget>
0022 #include <QPushButton>
0023 #include <QVBoxLayout>
0024 #include <QWindow>
0025 
0026 KNotePrintSelectedNotesDialog::KNotePrintSelectedNotesDialog(QWidget *parent)
0027     : QDialog(parent)
0028     , mListNotes(new QListWidget(this))
0029 {
0030     setWindowTitle(i18nc("@title:window", "Select notes"));
0031     auto mainLayout = new QVBoxLayout(this);
0032     mButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0033     QPushButton *okButton = mButtonBox->button(QDialogButtonBox::Ok);
0034     okButton->setDefault(true);
0035     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0036     mUser1Button = new QPushButton(this);
0037     mButtonBox->addButton(mUser1Button, QDialogButtonBox::ActionRole);
0038     connect(mButtonBox, &QDialogButtonBox::accepted, this, &KNotePrintSelectedNotesDialog::accept);
0039     connect(mButtonBox, &QDialogButtonBox::rejected, this, &KNotePrintSelectedNotesDialog::reject);
0040     auto w = new QWidget;
0041     auto vbox = new QVBoxLayout;
0042     vbox->setContentsMargins(0, 0, 0, 0);
0043     w->setLayout(vbox);
0044 
0045     mListNotes->setSelectionMode(QAbstractItemView::ExtendedSelection);
0046     vbox->addWidget(mListNotes);
0047 
0048     auto lay = new QHBoxLayout;
0049     lay->setContentsMargins(0, 0, 0, 0);
0050     vbox->addLayout(lay);
0051     auto lab = new QLabel(i18n("Printing theme:"), this);
0052     lay->addWidget(lab);
0053     mTheme = new KNotePrintSelectThemeComboBox(this);
0054     mTheme->loadThemes();
0055     lay->addWidget(mTheme);
0056 
0057     mUser1Button->setIcon(QIcon::fromTheme(QStringLiteral("document-print-preview")));
0058     mUser1Button->setText(i18n("Preview"));
0059     okButton->setIcon(QIcon::fromTheme(QStringLiteral("document-print")));
0060     okButton->setText(i18n("Print"));
0061     connect(mUser1Button, &QPushButton::clicked, this, &KNotePrintSelectedNotesDialog::slotPreview);
0062     connect(mListNotes, &QListWidget::itemSelectionChanged, this, &KNotePrintSelectedNotesDialog::slotSelectionChanged);
0063     mainLayout->addWidget(w);
0064     mainLayout->addWidget(mButtonBox);
0065     readConfig();
0066     slotSelectionChanged();
0067 }
0068 
0069 KNotePrintSelectedNotesDialog::~KNotePrintSelectedNotesDialog()
0070 {
0071     writeConfig();
0072 }
0073 
0074 void KNotePrintSelectedNotesDialog::slotSelectionChanged()
0075 {
0076     const bool hasSelection = (!mListNotes->selectedItems().isEmpty());
0077     mUser1Button->setEnabled(hasSelection);
0078     mButtonBox->button(QDialogButtonBox::Ok)->setEnabled(hasSelection);
0079 }
0080 
0081 void KNotePrintSelectedNotesDialog::setNotes(const QHash<Akonadi::Item::Id, KNote *> &notes)
0082 {
0083     mNotes = notes;
0084     QHashIterator<Akonadi::Item::Id, KNote *> i(notes);
0085     while (i.hasNext()) {
0086         i.next();
0087         auto item = new QListWidgetItem(mListNotes);
0088         item->setText(i.value()->name());
0089         item->setToolTip(i.value()->text());
0090         item->setData(AkonadiId, i.key());
0091     }
0092 }
0093 
0094 QList<KNotePrintObject *> KNotePrintSelectedNotesDialog::selectedNotes() const
0095 {
0096     QList<KNotePrintObject *> lstPrintObj;
0097     const QList<QListWidgetItem *> lst = mListNotes->selectedItems();
0098     for (QListWidgetItem *item : lst) {
0099         Akonadi::Item::Id akonadiId = item->data(AkonadiId).toLongLong();
0100         if (akonadiId != -1) {
0101             auto obj = new KNotePrintObject(mNotes.value(akonadiId)->item());
0102             lstPrintObj.append(obj);
0103         }
0104     }
0105     return lstPrintObj;
0106 }
0107 
0108 QString KNotePrintSelectedNotesDialog::selectedTheme() const
0109 {
0110     return mTheme->selectedTheme();
0111 }
0112 
0113 bool KNotePrintSelectedNotesDialog::preview() const
0114 {
0115     return mPreview;
0116 }
0117 
0118 namespace
0119 {
0120 static const char myKNotePrintSelectedNotesDialogName[] = "KNotePrintSelectedNotesDialog";
0121 }
0122 void KNotePrintSelectedNotesDialog::readConfig()
0123 {
0124     create(); // ensure a window is created
0125     windowHandle()->resize(QSize(300, 200));
0126     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myKNotePrintSelectedNotesDialogName));
0127     KWindowConfig::restoreWindowSize(windowHandle(), group);
0128     resize(windowHandle()->size()); // workaround for QTBUG-40584
0129 }
0130 
0131 void KNotePrintSelectedNotesDialog::writeConfig()
0132 {
0133     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myKNotePrintSelectedNotesDialogName));
0134     KWindowConfig::saveWindowSize(windowHandle(), group);
0135     group.sync();
0136 }
0137 
0138 void KNotePrintSelectedNotesDialog::slotPreview()
0139 {
0140     mPreview = true;
0141     accept();
0142 }
0143 
0144 #include "moc_knoteprintselectednotesdialog.cpp"