2018-06-26 14:09:39 +02:00
|
|
|
#include "EditableModelView.hpp"
|
2018-05-06 00:32:45 +02:00
|
|
|
|
|
|
|
#include <QAbstractTableModel>
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QHeaderView>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QTableView>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
EditableModelView::EditableModelView(QAbstractTableModel *model)
|
|
|
|
: tableView_(new QTableView(this))
|
|
|
|
, model_(model)
|
2018-05-06 00:32:45 +02:00
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
this->model_->setParent(this);
|
|
|
|
this->tableView_->setModel(model);
|
|
|
|
this->tableView_->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
|
|
this->tableView_->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
this->tableView_->verticalHeader()->hide();
|
2018-05-06 00:32:45 +02:00
|
|
|
|
|
|
|
// create layout
|
|
|
|
QVBoxLayout *vbox = new QVBoxLayout(this);
|
2018-05-06 15:39:03 +02:00
|
|
|
vbox->setMargin(0);
|
2018-05-06 00:32:45 +02:00
|
|
|
|
|
|
|
// create button layout
|
|
|
|
QHBoxLayout *buttons = new QHBoxLayout(this);
|
2019-08-26 14:07:21 +02:00
|
|
|
this->buttons_ = buttons;
|
2018-05-06 00:32:45 +02:00
|
|
|
vbox->addLayout(buttons);
|
|
|
|
|
|
|
|
// add
|
|
|
|
QPushButton *add = new QPushButton("Add");
|
|
|
|
buttons->addWidget(add);
|
2018-08-06 21:17:03 +02:00
|
|
|
QObject::connect(add, &QPushButton::clicked,
|
|
|
|
[this] { this->addButtonPressed.invoke(); });
|
2018-05-06 00:32:45 +02:00
|
|
|
|
|
|
|
// remove
|
|
|
|
QPushButton *remove = new QPushButton("Remove");
|
|
|
|
buttons->addWidget(remove);
|
|
|
|
QObject::connect(remove, &QPushButton::clicked, [this] {
|
|
|
|
QModelIndexList list;
|
2018-08-06 21:17:03 +02:00
|
|
|
while ((list = this->getTableView()->selectionModel()->selectedRows(0))
|
2018-10-21 13:43:02 +02:00
|
|
|
.length() > 0)
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
model_->removeRow(list[0].row());
|
2018-05-06 00:32:45 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-26 14:07:21 +02:00
|
|
|
buttons->addStretch();
|
|
|
|
|
|
|
|
// add tableview
|
|
|
|
vbox->addWidget(this->tableView_);
|
|
|
|
|
2018-05-06 00:32:45 +02:00
|
|
|
// finish button layout
|
|
|
|
buttons->addStretch(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditableModelView::setTitles(std::initializer_list<QString> titles)
|
|
|
|
{
|
|
|
|
int i = 0;
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const QString &title : titles)
|
|
|
|
{
|
|
|
|
if (this->model_->columnCount() == i)
|
|
|
|
{
|
2018-05-06 00:32:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
this->model_->setHeaderData(i++, Qt::Horizontal, title,
|
|
|
|
Qt::DisplayRole);
|
2018-05-06 00:32:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QTableView *EditableModelView::getTableView()
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->tableView_;
|
2018-05-06 00:32:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractTableModel *EditableModelView::getModel()
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->model_;
|
2018-05-06 00:32:45 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 14:07:21 +02:00
|
|
|
void EditableModelView::addCustomButton(QWidget *widget)
|
|
|
|
{
|
|
|
|
this->buttons_->addWidget(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditableModelView::addRegexHelpLink()
|
|
|
|
{
|
|
|
|
auto regexHelpLabel =
|
|
|
|
new QLabel("<a href='https://chatterino.com/help/regex'><span "
|
|
|
|
"style='color:#99f'>regex info</span></a>");
|
|
|
|
regexHelpLabel->setOpenExternalLinks(true);
|
|
|
|
this->addCustomButton(regexHelpLabel);
|
|
|
|
}
|
|
|
|
|
2018-05-06 00:32:45 +02:00
|
|
|
} // namespace chatterino
|