File indexing completed on 2025-01-05 04:54:20
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #include "knoteselectednotesdialog.h" 0007 #include "notes/knote.h" 0008 0009 #include <KConfigGroup> 0010 #include <KLocalizedString> 0011 0012 #include <KSharedConfig> 0013 #include <KWindowConfig> 0014 #include <QDialogButtonBox> 0015 #include <QListWidget> 0016 #include <QPushButton> 0017 #include <QVBoxLayout> 0018 #include <QWindow> 0019 0020 KNoteSelectedNotesDialog::KNoteSelectedNotesDialog(QWidget *parent) 0021 : QDialog(parent) 0022 { 0023 setWindowTitle(i18nc("@title:window", "Select notes")); 0024 auto mainLayout = new QVBoxLayout(this); 0025 mListNotes = new QListWidget(this); 0026 mListNotes->setSelectionMode(QAbstractItemView::ExtendedSelection); 0027 0028 connect(mListNotes, &QListWidget::itemSelectionChanged, this, &KNoteSelectedNotesDialog::slotSelectionChanged); 0029 0030 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0031 mOkButton = buttonBox->button(QDialogButtonBox::Ok); 0032 mOkButton->setDefault(true); 0033 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0034 connect(buttonBox, &QDialogButtonBox::accepted, this, &KNoteSelectedNotesDialog::accept); 0035 connect(buttonBox, &QDialogButtonBox::rejected, this, &KNoteSelectedNotesDialog::reject); 0036 0037 mainLayout->addWidget(mListNotes); 0038 mainLayout->addWidget(buttonBox); 0039 0040 readConfig(); 0041 slotSelectionChanged(); 0042 } 0043 0044 KNoteSelectedNotesDialog::~KNoteSelectedNotesDialog() 0045 { 0046 writeConfig(); 0047 } 0048 0049 void KNoteSelectedNotesDialog::slotSelectionChanged() 0050 { 0051 const bool hasSelection = (!mListNotes->selectedItems().isEmpty()); 0052 mOkButton->setEnabled(hasSelection); 0053 } 0054 0055 void KNoteSelectedNotesDialog::setNotes(const QHash<Akonadi::Item::Id, KNote *> ¬es) 0056 { 0057 mNotes = notes; 0058 QHashIterator<Akonadi::Item::Id, KNote *> i(notes); 0059 while (i.hasNext()) { 0060 i.next(); 0061 auto item = new QListWidgetItem(mListNotes); 0062 item->setText(i.value()->name()); 0063 item->setToolTip(i.value()->text()); 0064 item->setData(AkonadiId, i.key()); 0065 } 0066 } 0067 0068 QStringList KNoteSelectedNotesDialog::selectedNotes() const 0069 { 0070 QStringList lst; 0071 for (QListWidgetItem *item : mListNotes->selectedItems()) { 0072 Akonadi::Item::Id akonadiId = item->data(AkonadiId).toLongLong(); 0073 if (akonadiId != -1) { 0074 lst.append(QString::number(akonadiId)); 0075 } 0076 } 0077 return lst; 0078 } 0079 0080 namespace 0081 { 0082 static const char myKNoteSelectedNotesDialogName[] = "KNoteSelectedNotesDialog"; 0083 } 0084 void KNoteSelectedNotesDialog::readConfig() 0085 { 0086 create(); // ensure a window is created 0087 windowHandle()->resize(QSize(300, 200)); 0088 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myKNoteSelectedNotesDialogName)); 0089 KWindowConfig::restoreWindowSize(windowHandle(), group); 0090 resize(windowHandle()->size()); // workaround for QTBUG-40584 0091 } 0092 0093 void KNoteSelectedNotesDialog::writeConfig() 0094 { 0095 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myKNoteSelectedNotesDialogName)); 0096 KWindowConfig::saveWindowSize(windowHandle(), group); 0097 group.sync(); 0098 } 0099 0100 #include "moc_knoteselectednotesdialog.cpp"