created base for all the list based settings

This commit is contained in:
fourtf
2018-05-06 00:32:45 +02:00
parent 4c3f0921e2
commit ba4173822e
21 changed files with 646 additions and 317 deletions
+75
View File
@@ -0,0 +1,75 @@
#include "editablemodelview.hpp"
#include <QAbstractTableModel>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QPushButton>
#include <QTableView>
#include <QVBoxLayout>
namespace chatterino {
namespace widgets {
namespace helper {
EditableModelView::EditableModelView(QAbstractTableModel *_model)
: tableView(new QTableView(this))
, model(_model)
{
this->model->setParent(this);
this->tableView->setModel(_model);
this->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
this->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
this->tableView->verticalHeader()->hide();
// create layout
QVBoxLayout *vbox = new QVBoxLayout(this);
vbox->addWidget(this->tableView);
// create button layout
QHBoxLayout *buttons = new QHBoxLayout(this);
vbox->addLayout(buttons);
// add
QPushButton *add = new QPushButton("Add");
buttons->addWidget(add);
QObject::connect(add, &QPushButton::clicked, [this] { this->addButtonPressed.invoke(); });
// remove
QPushButton *remove = new QPushButton("Remove");
buttons->addWidget(remove);
QObject::connect(remove, &QPushButton::clicked, [this] {
QModelIndexList list;
while ((list = this->getTableView()->selectionModel()->selectedRows(0)).length() > 0) {
model->removeRow(list[0].row());
}
});
// finish button layout
buttons->addStretch(1);
}
void EditableModelView::setTitles(std::initializer_list<QString> titles)
{
int i = 0;
for (const QString &title : titles) {
if (this->model->columnCount() == i) {
break;
}
this->model->setHeaderData(i++, Qt::Horizontal, title, Qt::DisplayRole);
}
}
QTableView *EditableModelView::getTableView()
{
return this->tableView;
}
QAbstractTableModel *EditableModelView::getModel()
{
return this->model;
}
} // namespace helper
} // namespace widgets
} // namespace chatterino
+33
View File
@@ -0,0 +1,33 @@
#pragma once
#include <QWidget>
#include <pajlada/signals/signal.hpp>
class QAbstractTableModel;
class QTableView;
namespace chatterino {
namespace widgets {
namespace helper {
class EditableModelView : public QWidget
{
public:
EditableModelView(QAbstractTableModel *model);
void setTitles(std::initializer_list<QString> titles);
QTableView *getTableView();
QAbstractTableModel *getModel();
pajlada::Signals::NoArgSignal addButtonPressed;
private:
QTableView *tableView;
QAbstractTableModel *model;
};
} // namespace helper
} // namespace widgets
} // namespace chatterino
+10 -100
View File
@@ -1,5 +1,6 @@
#include "commandpage.hpp"
#include <QHeaderView>
#include <QLabel>
#include <QPushButton>
#include <QStandardItemModel>
@@ -11,6 +12,7 @@
#include "controllers/commands/commandmodel.hpp"
#include "util/layoutcreator.hpp"
#include "util/standarditemhelper.hpp"
#include "widgets/helper/editablemodelview.hpp"
//#include "widgets/helper/comboboxitemdelegate.hpp"
#include <QLabel>
@@ -34,107 +36,15 @@ CommandPage::CommandPage()
util::LayoutCreator<CommandPage> layoutCreator(this);
auto layout = layoutCreator.emplace<QVBoxLayout>().withoutMargin();
QTableView *view = *layout.emplace<QTableView>();
helper::EditableModelView *view =
*layout.emplace<helper::EditableModelView>(app->commands->createModel(nullptr));
auto *model = app->commands->createModel(view);
view->setModel(model);
model->setHeaderData(0, Qt::Horizontal, "Trigger");
model->setHeaderData(1, Qt::Horizontal, "Command");
view->setSelectionMode(QAbstractItemView::ExtendedSelection);
view->setSelectionBehavior(QAbstractItemView::SelectRows);
view->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
view->verticalHeader()->hide();
auto buttons = layout.emplace<QHBoxLayout>().withoutMargin();
{
auto add = buttons.emplace<QPushButton>("Add");
QObject::connect(*add, &QPushButton::clicked, [model, view] {
getApp()->commands->items.appendItem(
controllers::commands::Command{"/command", "I made a new command HeyGuys"});
view->scrollToBottom();
});
auto remove = buttons.emplace<QPushButton>("Remove");
QObject::connect(*remove, &QPushButton::clicked, [view, model] {
std::vector<int> indices;
for (const QModelIndex &index : view->selectionModel()->selectedRows(0)) {
indices.push_back(index.row());
}
std::sort(indices.begin(), indices.end());
for (int i = indices.size() - 1; i >= 0; i--) {
model->removeRow(indices[i]);
}
});
buttons->addStretch(1);
}
// QTableView *view = *layout.emplace<QTableView>();
// QStandardItemModel *model = new QStandardItemModel(0, 2, view);
// view->setModel(model);
// model->setHeaderData(0, Qt::Horizontal, "Trigger");
// model->setHeaderData(1, Qt::Horizontal, "Command");
// view->setSelectionMode(QAbstractItemView::ExtendedSelection);
// view->setSelectionBehavior(QAbstractItemView::SelectRows);
// view->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
// for (const QString &string : app->commands->getCommands()) {
// int index = string.indexOf(' ');
// if (index == -1) {
// model->appendRow({util::stringItem(string), util::stringItem("")});
// } else {
// model->appendRow(
// {util::stringItem(string.mid(0, index)), util::stringItem(string.mid(index +
// 1))});
// }
// }
// QObject::connect(
// model, &QStandardItemModel::dataChanged,
// [model](const QModelIndex &topLeft, const QModelIndex &bottomRight,
// const QVector<int> &roles) {
// QStringList list;
// for (int i = 0; i < model->rowCount(); i++) {
// QString command = model->item(i, 0)->data(Qt::EditRole).toString();
// // int index = command.indexOf(' ');
// // if (index != -1) {
// // command = command.mid(index);
// // }
// list.append(command + " " + model->item(i, 1)->data(Qt::EditRole).toString());
// }
// getApp()->commands->setCommands(list);
// });
// auto buttons = layout.emplace<QHBoxLayout>().withoutMargin();
// {
// auto add = buttons.emplace<QPushButton>("Add");
// QObject::connect(*add, &QPushButton::clicked, [model, view] {
// model->appendRow({util::stringItem("/command"), util::stringItem("")});
// view->scrollToBottom();
// });
// auto remove = buttons.emplace<QPushButton>("Remove");
// QObject::connect(*remove, &QPushButton::clicked, [view, model] {
// std::vector<int> indices;
// for (const QModelIndex &index : view->selectionModel()->selectedRows(0)) {
// indices.push_back(index.row());
// }
// std::sort(indices.begin(), indices.end());
// for (int i = indices.size() - 1; i >= 0; i--) {
// model->removeRow(indices[i]);
// }
// });
// buttons->addStretch(1);
// }
view->setTitles({"Trigger", "Command"});
view->getTableView()->horizontalHeader()->setStretchLastSection(true);
view->addButtonPressed.connect([] {
getApp()->commands->items.appendItem(
controllers::commands::Command{"/command", "I made a new command HeyGuys"});
});
layout.append(this->createCheckBox("Also match the trigger at the end of the message",
app->settings->allowCommandsAtEnd));
+13 -81
View File
@@ -1,10 +1,13 @@
#include "highlightingpage.hpp"
#include "application.hpp"
#include "controllers/highlights/highlightcontroller.hpp"
#include "controllers/highlights/highlightmodel.hpp"
#include "debug/log.hpp"
#include "singletons/settingsmanager.hpp"
#include "util/layoutcreator.hpp"
#include "util/standarditemhelper.hpp"
#include "widgets/helper/editablemodelview.hpp"
#include <QFileDialog>
#include <QListWidget>
@@ -41,95 +44,24 @@ HighlightingPage::HighlightingPage()
// HIGHLIGHTS
auto highlights = tabs.appendTab(new QVBoxLayout, "Highlights");
{
QTableView *view = *highlights.emplace<QTableView>();
auto *model = new QStandardItemModel(0, 4, view);
model->setHeaderData(0, Qt::Horizontal, "Pattern");
model->setHeaderData(1, Qt::Horizontal, "Flash taskbar");
model->setHeaderData(2, Qt::Horizontal, "Play sound");
model->setHeaderData(3, Qt::Horizontal, "Regex");
view->setModel(model);
view->setSelectionMode(QAbstractItemView::ExtendedSelection);
view->setSelectionBehavior(QAbstractItemView::SelectRows);
view->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
helper::EditableModelView *view = *highlights.emplace<helper::EditableModelView>(
app->highlights->createModel(nullptr));
// own name
auto *yourName = util::stringItem("Your name (automatic)", false, false);
yourName->setData(QBrush("#666"), Qt::ForegroundRole);
yourName->setFlags(yourName->flags() | Qt::ItemIsUserCheckable |
Qt::ItemIsUserCheckable);
yourName->setData(app->settings->enableHighlightsSelf ? 2 : 0, Qt::CheckStateRole);
model->appendRow(
{yourName,
util::boolItem(app->settings->enableHighlightTaskbar.getValue(), true, false),
util::boolItem(app->settings->enableHighlightSound.getValue(), true, false),
util::emptyItem()});
view->getTableView()->hideColumn(3);
// highlight phrases
// fourtf: could crash
for (const messages::HighlightPhrase &phrase :
app->settings->highlightProperties.getValue()) {
model->appendRow({util::stringItem(phrase.key), util::boolItem(phrase.alert),
util::boolItem(phrase.sound), util::boolItem(phrase.regex)});
}
view->setTitles({"Pattern", "Flash taskbar", "Play sound", "Regex"});
view->getTableView()->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
// fourtf: make class extrend BaseWidget and add this to dpiChanged
QTimer::singleShot(1, [view] {
view->resizeColumnsToContents();
view->setColumnWidth(0, 250);
view->getTableView()->resizeColumnsToContents();
view->getTableView()->setColumnWidth(0, 250);
});
auto buttons = highlights.emplace<QHBoxLayout>().withoutMargin();
QObject::connect(
model, &QStandardItemModel::dataChanged,
[model, app](const QModelIndex &topLeft, const QModelIndex &bottomRight,
const QVector<int> &roles) {
std::vector<messages::HighlightPhrase> phrases;
for (int i = 1; i < model->rowCount(); i++) {
phrases.push_back(messages::HighlightPhrase{
model->item(i, 0)->data(Qt::DisplayRole).toString(),
model->item(i, 1)->data(Qt::CheckStateRole).toBool(),
model->item(i, 2)->data(Qt::CheckStateRole).toBool(),
model->item(i, 3)->data(Qt::CheckStateRole).toBool()});
}
app->settings->highlightProperties.setValue(phrases);
app->settings->enableHighlightsSelf.setValue(
model->item(0, 0)->data(Qt::CheckStateRole).toBool());
app->settings->enableHighlightTaskbar.setValue(
model->item(0, 1)->data(Qt::CheckStateRole).toBool());
app->settings->enableHighlightSound.setValue(
model->item(0, 2)->data(Qt::CheckStateRole).toBool());
});
auto add = buttons.emplace<QPushButton>("Add");
QObject::connect(*add, &QPushButton::clicked, [model, view] {
model->appendRow({util::stringItem(""),
util::boolItem(model->item(model->rowCount() - 1, 1)
->data(Qt::CheckStateRole)
.toBool()),
util::boolItem(model->item(model->rowCount() - 1, 2)
->data(Qt::CheckStateRole)
.toBool()),
util::boolItem(false)});
view->scrollToBottom();
view->addButtonPressed.connect([] {
getApp()->highlights->phrases.appendItem(
controllers::highlights::HighlightPhrase{"my phrase", true, false, false});
});
auto remove = buttons.emplace<QPushButton>("Remove");
QObject::connect(*remove, &QPushButton::clicked, [view, model] {
std::vector<int> indices;
for (const QModelIndex &index : view->selectionModel()->selectedRows(0)) {
indices.push_back(index.row());
}
std::sort(indices.begin(), indices.end());
for (int i = indices.size() - 1; i >= 0; i--) {
model->removeRow(indices[i]);
}
});
buttons->addStretch(1);
view->hideColumn(3);
}
auto disabledUsers = tabs.appendTab(new QVBoxLayout, "Disabled Users");
{