Skip to content

Commit

Permalink
UI: Implemented checked list
Browse files Browse the repository at this point in the history
  • Loading branch information
Dax89 committed Mar 16, 2019
1 parent 666ad45 commit a3445a0
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 4 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Expand Up @@ -72,15 +72,20 @@ file(GLOB_RECURSE MODELS_SOURCES models/*.cpp)
file(GLOB_RECURSE RENDERER_HEADERS renderer/*.h)
file(GLOB_RECURSE RENDERER_SOURCES renderer/*.cpp)

# UI
file(GLOB_RECURSE UI_HEADERS ui/*.h)
file(GLOB_RECURSE UI_SOURCES ui/*.cpp)
file(GLOB_RECURSE UI_UIS ui/*.ui)

SET(HEADERS
${QHEXVIEW_HEADERS}
${REDASM_TEST_HEADERS}
${WIDGETS_HEADERS}
${DIALOGS_HEADERS}
${MODELS_HEADERS}
${RENDERER_HEADERS}
${UI_HEADERS}
mainwindow.h
redasmui.h
themeprovider.h
redasmsettings.h)

Expand All @@ -91,15 +96,16 @@ SET(SOURCES
${DIALOGS_SOURCES}
${MODELS_SOURCES}
${RENDERER_SOURCES}
${UI_SOURCES}
main.cpp
mainwindow.cpp
redasmui.cpp
themeprovider.cpp
redasmsettings.cpp)

set(FORMS
${WIDGETS_UIS}
${DIALOGS_UIS}
${UI_UIS}
mainwindow.ui)

set(RESOURCES
Expand Down
2 changes: 1 addition & 1 deletion LibREDasm
2 changes: 1 addition & 1 deletion mainwindow.cpp
Expand Up @@ -2,8 +2,8 @@
#include "ui_mainwindow.h"
#include "dialogs/settingsdialog/settingsdialog.h"
#include "dialogs/aboutdialog/aboutdialog.h"
#include "ui/redasmui.h"
#include "redasmsettings.h"
#include "redasmui.h"
#include "themeprovider.h"
#include <redasm/database/database.h>
#include <QWebEngineSettings>
Expand Down
49 changes: 49 additions & 0 deletions ui/dialogui.cpp
@@ -0,0 +1,49 @@
#include "dialogui.h"
#include "ui_dialogui.h"
#include <QSortFilterProxyModel>
#include <QPushButton>

DialogUI::DialogUI(QWidget *parent) : QDialog(parent), ui(new Ui::DialogUI)
{
ui->setupUi(this);

connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
}

DialogUI::~DialogUI() { delete ui; }
void DialogUI::setText(const QString &s) { ui->lblTitle->setText(s); }

void DialogUI::setItems(REDasm::UI::CheckList &items)
{
this->createList(new CheckedItemsModel(items));

connect(ui->pbApply, &QPushButton::clicked, this, [&]() {
QListView* lvitems = static_cast<QListView*>(ui->stackedWidget->currentWidget());
QSortFilterProxyModel* filtermodel = static_cast<QSortFilterProxyModel*>(lvitems->model());
CheckedItemsModel* checkeditemsmodel = static_cast<CheckedItemsModel*>(filtermodel->sourceModel());

checkeditemsmodel->uncheckAll();

for(int i = 0; i < filtermodel->rowCount(); i++)
filtermodel->setData(filtermodel->index(i, 0), Qt::Checked, Qt::CheckStateRole);
});
}

void DialogUI::createList(QAbstractItemModel* model)
{
QSortFilterProxyModel* filtermodel = new QSortFilterProxyModel(this);
filtermodel->setFilterCaseSensitivity(Qt::CaseInsensitive);
filtermodel->setSourceModel(model);

QListView* lvitems = new QListView(this);
lvitems->setUniformItemSizes(true);
model->setParent(lvitems);

lvitems->setModel(filtermodel);
ui->stackedWidget->addWidget(lvitems);

connect(ui->leFilter, &QLineEdit::textChanged, filtermodel, &QSortFilterProxyModel::setFilterFixedString);
}

void DialogUI::setCanAccept(bool b) { ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(b); }

33 changes: 33 additions & 0 deletions ui/dialogui.h
@@ -0,0 +1,33 @@
#ifndef DIALOGUI_H
#define DIALOGUI_H

#include <QListView>
#include <QDialog>
#include <redasm/redasm_ui.h>
#include "models/checkeditemsmodel.h"

namespace Ui {
class DialogUI;
}

class DialogUI : public QDialog
{
Q_OBJECT

public:
explicit DialogUI(QWidget *parent = nullptr);
~DialogUI();

public:
void setText(const QString& s);
void setItems(REDasm::UI::CheckList& items);

private:
void createList(QAbstractItemModel *model);
void setCanAccept(bool b);

private:
Ui::DialogUI *ui;
};

#endif // DIALOGUI_H
98 changes: 98 additions & 0 deletions ui/dialogui.ui
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DialogUI</class>
<widget class="QDialog" name="DialogUI">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>481</width>
<height>392</height>
</rect>
</property>
<property name="windowTitle">
<string>DialogUI</string>
</property>
<property name="modal">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,0,1,0">
<item>
<widget class="QLabel" name="lblTitle">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="leFilter">
<property name="text">
<string/>
</property>
<property name="placeholderText">
<string>Filter...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pbApply">
<property name="text">
<string>Apply</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QStackedWidget" name="stackedWidget"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>DialogUI</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>DialogUI</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
41 changes: 41 additions & 0 deletions ui/models/checkeditemsmodel.cpp
@@ -0,0 +1,41 @@
#include "checkeditemsmodel.h"

CheckedItemsModel::CheckedItemsModel(REDasm::UI::CheckList &items, QObject *parent): QAbstractListModel(parent), m_items(items) { }

void CheckedItemsModel::uncheckAll()
{
this->beginResetModel();

for(auto& item : m_items)
item.second = false;

this->endResetModel();
}

Qt::ItemFlags CheckedItemsModel::flags(const QModelIndex &index) const { return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable; }
int CheckedItemsModel::rowCount(const QModelIndex &parent) const { return m_items.size(); }

QVariant CheckedItemsModel::data(const QModelIndex &index, int role) const
{
const auto& item = m_items[index.row()];

if(role == Qt::DisplayRole)
return QString::fromStdString(item.first);
if(role == Qt::CheckStateRole)
return item.second ? Qt::Checked : Qt::Unchecked;

return QVariant();
}

bool CheckedItemsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(role == Qt::CheckStateRole)
{
auto& item = m_items[index.row()];
item.second = value == Qt::Checked ? true : false;
emit dataChanged(index, index);
return true;
}

return QAbstractListModel::setData(index, value, role);
}
23 changes: 23 additions & 0 deletions ui/models/checkeditemsmodel.h
@@ -0,0 +1,23 @@
#ifndef CHECKEDITEMSMODEL_H
#define CHECKEDITEMSMODEL_H

#include <QAbstractListModel>
#include <redasm/redasm_ui.h>

class CheckedItemsModel : public QAbstractListModel
{
public:
CheckedItemsModel(REDasm::UI::CheckList& items, QObject* parent = nullptr);
void uncheckAll();

public:
Qt::ItemFlags flags(const QModelIndex &index) const;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);

private:
REDasm::UI::CheckList& m_items;
};

#endif // CHECKEDITEMSMODEL_H
10 changes: 10 additions & 0 deletions redasmui.cpp → ui/redasmui.cpp
@@ -1,8 +1,18 @@
#include "redasmui.h"
#include "dialogui.h"
#include <QMessageBox>

REDasmUI::REDasmUI(QMainWindow *mainwindow): m_mainwindow(mainwindow) { }

void REDasmUI::checkList(const std::string &title, const std::string &text, std::deque<REDasm::UI::CheckListItem> &items)
{
DialogUI dlgui(m_mainwindow);
dlgui.setWindowTitle(QString::fromStdString(title));
dlgui.setText(QString::fromStdString(text));
dlgui.setItems(items);
dlgui.exec();
}

bool REDasmUI::askYN(const std::string &title, const std::string &text)
{
int res = QMessageBox::question(m_mainwindow,
Expand Down
1 change: 1 addition & 0 deletions redasmui.h → ui/redasmui.h
Expand Up @@ -9,6 +9,7 @@ class REDasmUI: public REDasm::AbstractUI
public:
REDasmUI(QMainWindow* mainwindow);
virtual ~REDasmUI() = default;
virtual void checkList(const std::string& title, const std::string& text, std::deque<REDasm::UI::CheckListItem>& items);
virtual bool askYN(const std::string& title, const std::string& text);

private:
Expand Down

0 comments on commit a3445a0

Please sign in to comment.