Skip to content

Commit

Permalink
Persist Search History
Browse files Browse the repository at this point in the history
Persist the search history even across DLT Viewer restarts.

Signed-off by : Pavithra Anand <[email protected]>
  • Loading branch information
Pavithra-Anand committed Feb 18, 2025
1 parent 8840a1c commit 8cd026a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
28 changes: 27 additions & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <QtEndian>
#include <QDir>
#include <QDirIterator>
#include <QCompleter>

/**
* From QDlt.
Expand Down Expand Up @@ -101,6 +102,8 @@ MainWindow::MainWindow(QWidget *parent) :

target_version_string = "";

loadSearchHistoryList(searchHistory);

initState();

/* Apply loaded settings */
Expand Down Expand Up @@ -505,6 +508,10 @@ void MainWindow::initView()
searchInput = new SearchForm;
connect(searchInput, &SearchForm::abortSearch, searchDlg, &SearchDialog::abortSearch);
searchDlg->appendLineEdit(searchInput->input());
searchLineEdit = searchInput->input();
searchComboBox = searchInput->getComboBox();
searchComboBox->addItems(searchHistory);
searchLineEdit->clear();

connect(searchInput->input(), SIGNAL(textChanged(QString)),searchDlg,SLOT(textEditedFromToolbar(QString)));
connect(searchInput->input(), SIGNAL(returnPressed()), this, SLOT(on_actionFindNext()));
Expand Down Expand Up @@ -1034,6 +1041,9 @@ void MainWindow::closeEvent(QCloseEvent *event)
{
QMainWindow::closeEvent(event);
}
if(searchDlg){
searchDlg->saveSearchHistory(searchHistory);
}
}


Expand Down Expand Up @@ -1220,7 +1230,7 @@ bool MainWindow::openDltFile(QStringList fileNames)

// clear the cache stored for the history
searchDlg->clearCacheHistory();

onAddActionToHistory();
if(outputfile.isOpen())
{
if (outputfile.size() == 0)
Expand Down Expand Up @@ -8266,6 +8276,9 @@ void MainWindow::onAddActionToHistory()
{
searchHistory.prepend(searchText);
}
searchCompleter = new QCompleter(searchHistory, this);
searchCompleter->setCaseSensitivity(Qt::CaseInsensitive);
searchLineEdit->setCompleter(searchCompleter);

int searchHistorySize = searchHistory.size();
for (int i = 0;i < searchHistorySize && i < MaxSearchHistory; i++)
Expand All @@ -8292,6 +8305,19 @@ void MainWindow::onSearchProgressChanged(bool isInProgress)
ui->dockWidgetProject->setEnabled(!isInProgress);
}

void MainWindow::loadSearchHistoryList(QStringList& searchHistory)
{
//To load the search history back into the application once app restarts
QSettings settings("MyApp", "SearchHistory");
searchHistory.clear();
int size = settings.beginReadArray("history");
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
searchHistory.append(settings.value("entry").toString());
}
settings.endArray();
}

QString MainWindow::GetConnectionType(int iTypeNumber)
{
QString port;
Expand Down
7 changes: 5 additions & 2 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ class MainWindow : public QMainWindow
QStringList autoloadPluginsVersionStrings;

/* String List Containing Search History */
enum { MaxSearchHistory = 50 };
enum { MaxSearchHistory = 20 };
QAction *searchHistoryActs[MaxSearchHistory];
QStringList searchHistory;

QLineEdit* searchLineEdit;
QComboBox* searchComboBox;
QCompleter* searchCompleter;

/* Recent files */
enum { MaxRecentFiles = 5 };
Expand Down Expand Up @@ -596,6 +598,7 @@ public slots:
//History Slots
void onAddActionToHistory();
void onSearchProgressChanged(bool isInProgress);
void loadSearchHistoryList(QStringList &searchHistory);

void handleImportResults(const QString &);
void handleExportResults(const QString &);
Expand Down
12 changes: 12 additions & 0 deletions src/searchdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,18 @@ void SearchDialog::clearCacheHistory()
cachedHistoryKey.clear();
}

void SearchDialog::saveSearchHistory(QStringList& searchHistory) {
//To save the search history
QSettings settings("MyApp", "SearchHistory");
settings.beginWriteArray("history");
int count = qMin(searchHistory.size(), 20);
for (int i = 0; i < count; ++i) {
settings.setArrayIndex(i);
settings.setValue("entry", searchHistory.at(i));
}
settings.endArray();
}

void SearchDialog::on_checkBoxHeader_toggled(bool checked)
{
QDltSettingsManager::getInstance()->setValue("other/search/checkBoxHeader", checked);
Expand Down
1 change: 1 addition & 0 deletions src/searchdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public slots:
void findPreviousClicked();
void loadSearchHistory();
void abortSearch();
void saveSearchHistory(QStringList &searchHistory);

signals:
void refreshedSearchIndex();
Expand Down
5 changes: 5 additions & 0 deletions src/searchform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ QLineEdit *SearchForm::input() const
return ui->comboBox->lineEdit();
}

QComboBox *SearchForm::getComboBox() const
{
return ui->comboBox;
}

void SearchForm::setState(State state)
{
switch(state) {
Expand Down
2 changes: 2 additions & 0 deletions src/searchform.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QWidget>
#include <QStringListModel>
#include <QComboBox>

namespace Ui {
class SearchForm;
Expand Down Expand Up @@ -30,6 +31,7 @@ class SearchForm : public QWidget
void setProgress(int val);
void resetProgress();
void updateHistory();
QComboBox* getComboBox() const;

signals:
void abortSearch();
Expand Down

0 comments on commit 8cd026a

Please sign in to comment.