File indexing completed on 2024-04-14 15:51:31

0001 /***************************************************************************
0002                        exthistorycombo.cpp  -  description
0003                              -------------------
0004     begin                : Sun May 20 2007
0005     copyright            : (C) 2007 by Dominik Seichter
0006     email                : domseichter@web.de
0007 ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "exthistorycombo.h"
0019 
0020 #include <kconfig.h>
0021 #include <kconfiggroup.h>
0022 #include <KSharedConfig>
0023 
0024 #include <QLineEdit>
0025 
0026 #define EXT_HISTORY_COMBO_MAX_COUNT 10
0027 #define EXT_HISTORY_COMBO_TIMER_DELAY 500
0028 
0029 ExtHistoryCombo::ExtHistoryCombo(QWidget *parent)
0030     : KHistoryComboBox(parent)
0031 {
0032     connect(this, static_cast<void (ExtHistoryCombo::*)(const QString &)>(&ExtHistoryCombo::activated),
0033             this, &ExtHistoryCombo::addToHistory);
0034     connect(this, &ExtHistoryCombo::editTextChanged,
0035             this, &ExtHistoryCombo::slotTextChanged);
0036     connect(&m_timer, &QTimer::timeout,
0037             this, &ExtHistoryCombo::delayedTextChanged);
0038 
0039     this->setMaxCount(EXT_HISTORY_COMBO_MAX_COUNT);
0040     this->setDuplicatesEnabled(false);
0041 
0042     m_timer.setSingleShot(true);
0043 }
0044 
0045 void ExtHistoryCombo::slotTextChanged()
0046 {
0047     m_timer.stop();
0048     m_timer.start(EXT_HISTORY_COMBO_TIMER_DELAY);
0049 }
0050 
0051 void ExtHistoryCombo::loadConfig()
0052 {
0053     QString currentText = this->currentText();
0054     QStringList history;
0055     QStringList completion;
0056 
0057     KSharedConfigPtr config = KSharedConfig::openConfig();
0058     KConfigGroup groupGui = config->group(QString("ExtHistoryCombo") + this->objectName());
0059 
0060     completion = groupGui.readEntry("CompletionList", QStringList());
0061     history = groupGui.readEntry("HistoryList", QStringList());
0062 
0063     this->completionObject()->setItems(completion);
0064     this->setHistoryItems(history);
0065     this->lineEdit()->setText(currentText); // Preserve current text
0066 }
0067 
0068 void ExtHistoryCombo::saveConfig()
0069 {
0070     addToHistory(currentText());
0071 
0072     KSharedConfigPtr config = KSharedConfig::openConfig();
0073     KConfigGroup groupGui = config->group(QString("ExtHistoryCombo") + this->objectName());
0074 
0075     groupGui.writeEntry("CompletionList", this->completionObject()->items());
0076     groupGui.writeEntry("HistoryList", this->historyItems());
0077 
0078     config->sync();
0079 }
0080 
0081 void ExtHistoryCombo::selectAll()
0082 {
0083     this->lineEdit()->setSelection(0, this->lineEdit()->text().length());
0084 }