File indexing completed on 2022-09-27 16:33:50

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "exthistorycombo.h"
0005 
0006 #include <kconfig.h>
0007 #include <kconfiggroup.h>
0008 #include <KSharedConfig>
0009 
0010 #include <QLineEdit>
0011 
0012 #define EXT_HISTORY_COMBO_MAX_COUNT 10
0013 #define EXT_HISTORY_COMBO_TIMER_DELAY 500
0014 
0015 ExtHistoryCombo::ExtHistoryCombo(QWidget *parent)
0016     : KHistoryComboBox(parent)
0017 {
0018     connect(this, static_cast<void (ExtHistoryCombo::*)(const QString &)>(&ExtHistoryCombo::activated),
0019             this, &ExtHistoryCombo::addToHistory);
0020     connect(this, &ExtHistoryCombo::editTextChanged,
0021             this, &ExtHistoryCombo::slotTextChanged);
0022     connect(&m_timer, &QTimer::timeout,
0023             this, &ExtHistoryCombo::delayedTextChanged);
0024 
0025     this->setMaxCount(EXT_HISTORY_COMBO_MAX_COUNT);
0026     this->setDuplicatesEnabled(false);
0027 
0028     m_timer.setSingleShot(true);
0029 }
0030 
0031 void ExtHistoryCombo::slotTextChanged()
0032 {
0033     m_timer.stop();
0034     m_timer.start(EXT_HISTORY_COMBO_TIMER_DELAY);
0035 }
0036 
0037 void ExtHistoryCombo::loadConfig()
0038 {
0039     QString currentText = this->currentText();
0040     QStringList history;
0041     QStringList completion;
0042 
0043     KSharedConfigPtr config = KSharedConfig::openConfig();
0044     KConfigGroup groupGui = config->group(QString("ExtHistoryCombo") + this->objectName());
0045 
0046     completion = groupGui.readEntry("CompletionList", QStringList());
0047     history = groupGui.readEntry("HistoryList", QStringList());
0048 
0049     this->completionObject()->setItems(completion);
0050     this->setHistoryItems(history);
0051     this->lineEdit()->setText(currentText); // Preserve current text
0052 }
0053 
0054 void ExtHistoryCombo::saveConfig()
0055 {
0056     addToHistory(currentText());
0057 
0058     KSharedConfigPtr config = KSharedConfig::openConfig();
0059     KConfigGroup groupGui = config->group(QString("ExtHistoryCombo") + this->objectName());
0060 
0061     groupGui.writeEntry("CompletionList", this->completionObject()->items());
0062     groupGui.writeEntry("HistoryList", this->historyItems());
0063 
0064     config->sync();
0065 }
0066 
0067 void ExtHistoryCombo::selectAll()
0068 {
0069     this->lineEdit()->setSelection(0, this->lineEdit()->text().length());
0070 }