Commit 1836a69a authored by Alexander Semke's avatar Alexander Semke
Browse files

Allow to search/filter for variable names and disable the save and delete...

Allow to search/filter for variable names and disable the save and delete buttons in the variable manager if no variables are available.
parent 7c65ae88
Pipeline #63104 passed with stage
in 24 minutes and 51 seconds
/*
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2010 Alexander Rieder <alexanderrieder@gmail.com>
SPDX-FileCopyrightText: 2018-2021 Alexander Semke <alexander.semke@web.de>
*/
#include "variablemanagerwidget.h"
#include "backend.h"
#include "extension.h"
#include "session.h"
#include "ui_newvardlg.h"
#include <QAction>
#include <QDialog>
#include <QFileDialog>
#include <QMenu>
#include <QPushButton>
#include <QTimer>
#include <QToolButton>
#include <QTreeView>
#include <KIconLoader>
#include <KMessageBox>
#include "session.h"
#include "extension.h"
#include "backend.h"
#include "ui_newvardlg.h"
VariableManagerWidget::VariableManagerWidget(Cantor::Session* session, QWidget* parent) : QWidget(parent),
m_table(new QTreeView(this))
m_treeView(new QTreeView(this))
{
auto* layout = new QVBoxLayout(this);
layout->addWidget(m_table, 1);
layout->addWidget(m_treeView, 1);
m_table->setRootIsDecorated(false);
m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_table->setAlternatingRowColors(true);
m_treeView->setRootIsDecorated(false);
m_treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_treeView->setAlternatingRowColors(true);
auto* btnLayout = new QHBoxLayout();
btnLayout->setSpacing(0);
btnLayout->setMargin(0);
auto* spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
btnLayout->addItem(spacer);
//Buttons to save/load the variables
int size = KIconLoader::global()->currentSize(KIconLoader::MainToolbar);
m_newBtn = new QToolButton(this);
m_newBtn->setIcon(QIcon::fromTheme(QLatin1String("document-new")));
m_newBtn->setToolTip(i18n("Add new variable"));
m_newBtn->setToolTip(i18n("Add New Variable"));
m_newBtn->setIconSize(QSize(size, size));
connect(m_newBtn, &QToolButton::clicked, this, &VariableManagerWidget::newVariable);
btnLayout->addWidget(m_newBtn);
m_loadBtn=new QToolButton(this);
m_loadBtn->setIcon(QIcon::fromTheme(QLatin1String("document-open")));
m_loadBtn->setToolTip(i18n("Load Variables"));
m_loadBtn->setIconSize(QSize(size, size));
connect(m_loadBtn, &QToolButton::clicked, this, &VariableManagerWidget::load);
btnLayout->addWidget(m_loadBtn);
m_saveBtn = new QToolButton(this);
m_saveBtn->setIcon(QIcon::fromTheme(QLatin1String("document-save")));
m_saveBtn->setToolTip(i18n("Store Variables"));
m_saveBtn->setToolTip(i18n("Save Variables"));
m_saveBtn->setIconSize(QSize(size, size));
connect(m_saveBtn, &QToolButton::clicked, this, &VariableManagerWidget::save);
btnLayout->addWidget(m_saveBtn);
m_clearBtn = new QToolButton(this);
m_clearBtn->setIcon(QIcon::fromTheme(QLatin1String("edit-clear")));
m_clearBtn->setToolTip(i18n("Clear Variables"));
m_clearBtn->setIcon(QIcon::fromTheme(QLatin1String("edit-delete")));
m_clearBtn->setToolTip(i18n("Remove Variables"));
m_clearBtn->setIconSize(QSize(size, size));
connect(m_clearBtn, &QToolButton::clicked, this, &VariableManagerWidget::clearVariables);
btnLayout->addWidget(m_clearBtn);
auto* spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
btnLayout->addItem(spacer);
//Filter
m_leFilter = new QLineEdit(this);
m_leFilter->setClearButtonEnabled(true);
m_leFilter->setPlaceholderText(i18n("Search/Filter"));
btnLayout->addWidget(m_leFilter);
m_bFilterOptions = new QToolButton(this);
m_bFilterOptions->setIcon(QIcon::fromTheme(QLatin1String("configure")));
m_bFilterOptions->setCheckable(true);
btnLayout->addWidget(m_bFilterOptions);
layout->addLayout(btnLayout);
//actions
m_caseSensitiveAction = new QAction(i18n("Case Sensitive"), this);
m_caseSensitiveAction->setCheckable(true);
m_caseSensitiveAction->setChecked(false);
m_matchCompleteWordAction = new QAction(i18n("Match Complete Word"), this);
m_matchCompleteWordAction->setCheckable(true);
m_matchCompleteWordAction->setChecked(false);
//signal-slot connections
connect(m_leFilter, &QLineEdit::textChanged, this, &VariableManagerWidget::filterTextChanged);
connect(m_bFilterOptions, &QPushButton::toggled, this, &VariableManagerWidget::toggleFilterOptionsMenu);
connect(m_caseSensitiveAction, &QAction::triggered, this, [=]() {filterTextChanged(m_leFilter->text());} );
connect(m_matchCompleteWordAction, &QAction::triggered, this, [=]() {filterTextChanged(m_leFilter->text());});
connect(m_newBtn, &QToolButton::clicked, this, &VariableManagerWidget::newVariable);
connect(m_loadBtn, &QToolButton::clicked, this, &VariableManagerWidget::load);
connect(m_saveBtn, &QToolButton::clicked, this, &VariableManagerWidget::save);
connect(m_clearBtn, &QToolButton::clicked, this, &VariableManagerWidget::clearVariables);
setSession(session);
}
......@@ -78,8 +108,12 @@ void VariableManagerWidget::setSession(Cantor::Session* session)
if (session)
{
m_model = session->variableDataModel();
if (m_table)
m_table->setModel(m_model);
if (m_treeView)
m_treeView->setModel(m_model);
connect(m_model, &QAbstractItemModel::rowsInserted, this, &VariableManagerWidget::updateButtons);
connect(m_model, &QAbstractItemModel::rowsRemoved, this, &VariableManagerWidget::updateButtons);
updateButtons();
//check for the methods the backend actually supports, and disable the buttons accordingly
auto* ext = dynamic_cast<Cantor::VariableManagementExtension*>(
......@@ -122,6 +156,10 @@ void VariableManagerWidget::clearVariables()
//DefaultVariableModel. If our model isn't one of those,
//this call will just do nothing.
QMetaObject::invokeMethod(m_model, "clearVariables", Qt::QueuedConnection);
//QAbstractItemModel::rowsRemoved() doesn't seem to be sent in this case,
//call updateButtons explicitly
QTimer::singleShot(0, this, [=] () { updateButtons(); });
}
}
......@@ -156,6 +194,9 @@ void VariableManagerWidget::load()
void VariableManagerWidget::newVariable()
{
QPointer<QDialog> dlg = new QDialog(this);
dlg->setWindowTitle(i18n("Add New Variable"));
dlg->setWindowIcon(QIcon::fromTheme(QLatin1String("document-new")));
QWidget* widget = new QWidget(dlg);
Ui::NewVariableDialogBase base;
base.setupUi(widget);
......@@ -186,3 +227,46 @@ void VariableManagerWidget::newVariable()
delete dlg;
}
/*!
toggles the menu for the filter/search options
*/
void VariableManagerWidget::toggleFilterOptionsMenu(bool checked) {
if (checked) {
QMenu menu;
menu.addAction(m_caseSensitiveAction);
menu.addAction(m_matchCompleteWordAction);
connect(&menu, &QMenu::aboutToHide, m_bFilterOptions, &QPushButton::toggle);
menu.exec(m_bFilterOptions->mapToGlobal(QPoint(0, m_bFilterOptions->height())));
}
}
/*!
called when the filter/search text was changed.
*/
void VariableManagerWidget::filterTextChanged(const QString& text) {
auto sensitivity = m_caseSensitiveAction->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
bool matchCompleteWord = m_matchCompleteWordAction->isChecked();
const auto* model = m_treeView->model();
for (int i = 0; i < model->rowCount(); i++) {
const auto& child = model->index(i, 0);
const auto& name = model->data(child).toString();
bool visible = true;
if (text.isEmpty())
visible = true;
else if (matchCompleteWord)
visible = name.startsWith(text, sensitivity);
else
visible = name.contains(text, sensitivity);
m_treeView->setRowHidden(i, QModelIndex(), !visible);
}
}
void VariableManagerWidget::updateButtons()
{
bool enabled = (m_treeView->model()->rowCount() != 0);
m_saveBtn->setEnabled(enabled);
m_clearBtn->setEnabled(enabled);
}
/*
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2010 Alexander Rieder <alexanderrieder@gmail.com>
SPDX-FileCopyrightText: 2021 Alexander Semke <alexander.semke@web.de>
*/
#ifndef _VARIABLEMANAGERWIDGET_H
......@@ -9,40 +10,50 @@
#include <QWidget>
namespace Cantor{
class Session;
class Session;
}
class QTreeView;
class QToolButton;
class QAbstractItemModel;
class QLineEdit;
class QToolButton;
class QTreeView;
class VariableManagerWidget : public QWidget
{
Q_OBJECT
public:
public:
VariableManagerWidget(Cantor::Session*, QWidget*);
~VariableManagerWidget() override = default;
void setSession(Cantor::Session*);
public Q_SLOTS:
public Q_SLOTS:
void clearVariables();
void save();
void load();
void newVariable();
Q_SIGNALS:
Q_SIGNALS:
void runCommand(const QString&);
private:
private:
Cantor::Session* m_session{nullptr};
QAbstractItemModel* m_model{nullptr};
QTreeView* m_table{nullptr};
QTreeView* m_treeView{nullptr};
QToolButton* m_newBtn{nullptr};
QToolButton* m_loadBtn{nullptr};
QToolButton* m_saveBtn{nullptr};
QToolButton* m_clearBtn{nullptr};
QLineEdit* m_leFilter;
QToolButton* m_bFilterOptions;
QAction* m_caseSensitiveAction;
QAction* m_matchCompleteWordAction;
private Q_SLOTS:
void filterTextChanged(const QString&);
void toggleFilterOptionsMenu(bool);
void updateButtons();
};
#endif /* _VARIABLEMANAGERWIDGET_H */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment