added new TupleTableModel for settingsdialog

This commit is contained in:
fourtf
2018-04-25 14:49:30 +02:00
parent 1edcfe5219
commit 859f4aefcb
18 changed files with 574 additions and 236 deletions
+112
View File
@@ -0,0 +1,112 @@
#include "attachedwindow.hpp"
#include <QTimer>
#include <QVBoxLayout>
#include "widgets/split.hpp"
#include "Windows.h"
#pragma comment(lib, "Dwmapi.lib")
namespace chatterino {
namespace widgets {
AttachedWindow::AttachedWindow(void *_target, int _yOffset)
: target(_target)
, yOffset(_yOffset)
, QWidget(nullptr, Qt::FramelessWindowHint | Qt::Window)
{
QLayout *layout = new QVBoxLayout(this);
layout->setMargin(0);
this->setLayout(layout);
auto *split = new Split(singletons::ThemeManager::getInstance(), this);
this->ui.split = split;
split->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::MinimumExpanding);
layout->addWidget(split);
}
AttachedWindow::~AttachedWindow()
{
for (auto it = items.begin(); it != items.end(); it++) {
if (it->window == this) {
items.erase(it);
break;
}
}
}
AttachedWindow *AttachedWindow::get(void *target, const QString &winId, int yOffset)
{
for (Item &item : items) {
if (item.hwnd == target) {
return item.window;
}
}
auto *window = new AttachedWindow(target, yOffset);
items.push_back(Item{target, window, winId});
return window;
}
void AttachedWindow::detach(const QString &winId)
{
for (Item &item : items) {
if (item.winId == winId) {
item.window->deleteLater();
}
}
}
void AttachedWindow::setChannel(ChannelPtr channel)
{
this->ui.split->setChannel(channel);
}
void AttachedWindow::showEvent(QShowEvent *)
{
attachToHwnd(this->target);
}
void AttachedWindow::attachToHwnd(void *_hwnd)
{
QTimer *timer = new QTimer(this);
timer->setInterval(1);
HWND hwnd = (HWND)this->winId();
HWND attached = (HWND)_hwnd;
QObject::connect(timer, &QTimer::timeout, [this, hwnd, attached, timer] {
::SetLastError(0);
RECT xD;
::GetWindowRect(attached, &xD);
if (::GetLastError() != 0) {
timer->stop();
timer->deleteLater();
this->deleteLater();
}
HWND next = ::GetNextWindow(attached, GW_HWNDPREV);
::SetWindowPos(hwnd, next ? next : HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
::MoveWindow(hwnd, xD.right - 360, xD.top + this->yOffset - 8, 360 - 8,
xD.bottom - xD.top - this->yOffset, false);
// ::MoveWindow(hwnd, xD.right - 360, xD.top + 82, 360 - 8, xD.bottom - xD.top - 82 -
// 8,
// false);
});
timer->start();
}
// void AttachedWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
//{
// MSG *msg = reinterpret_cast
// case WM_NCCALCSIZE: {
// }
//}
std::vector<AttachedWindow::Item> AttachedWindow::items;
} // namespace widgets
} // namespace chatterino
+48
View File
@@ -0,0 +1,48 @@
#pragma once
#include <QWidget>
#include "channel.hpp"
#include "widgets/split.hpp"
namespace chatterino {
namespace widgets {
class AttachedWindow : public QWidget
{
AttachedWindow(void *target, int asdf);
public:
~AttachedWindow();
static AttachedWindow *get(void *target, const QString &winId, int yOffset);
static void detach(const QString &winId);
void setChannel(ChannelPtr channel);
protected:
virtual void showEvent(QShowEvent *) override;
// virtual void nativeEvent(const QByteArray &eventType, void *message, long *result)
// override;
private:
void *target;
int yOffset;
struct {
Split *split;
} ui;
void attachToHwnd(void *hwnd);
struct Item {
void *hwnd;
AttachedWindow *window;
QString winId;
};
static std::vector<Item> items;
};
} // namespace widgets
} // namespace chatterino
+1 -1
View File
@@ -30,7 +30,7 @@ public:
QSize getScaleIndependantSize() const;
int getScaleIndependantWidth() const;
int getScaleIndependantHeight() const;
void setScaleIndependantSize(int width, int height);
void setScaleIndependantSize(int width, int yOffset);
void setScaleIndependantSize(QSize);
void setScaleIndependantWidth(int value);
void setScaleIndependantHeight(int value);
+5 -5
View File
@@ -98,7 +98,7 @@ ChannelView::ChannelView(BaseWidget *parent)
// this->resizeEvent(e);
// delete e;
this->scrollBar.resize(this->scrollBar.width(), height() + 1);
this->scrollBar.resize(this->scrollBar.width(), this->height() + 1);
singletons::SettingManager::getInstance().showLastMessageIndicator.connect(
[this](auto, auto) { this->update(); }, this->managedConnections);
@@ -198,14 +198,14 @@ void ChannelView::actuallyLayoutMessages()
y += message->getHeight();
if (y >= height()) {
if (y >= this->height()) {
break;
}
}
}
// layout the messages at the bottom to determine the scrollbar thumb size
int h = height() - 8;
int h = this->height() - 8;
for (int i = (int)messagesSnapshot.getLength() - 1; i >= 0; i--) {
auto *message = messagesSnapshot[i].get();
@@ -472,7 +472,7 @@ void ChannelView::updateLastReadMessage()
void ChannelView::resizeEvent(QResizeEvent *)
{
this->scrollBar.resize(this->scrollBar.width(), height());
this->scrollBar.resize(this->scrollBar.width(), this->height());
this->scrollBar.move(this->width() - this->scrollBar.width(), 0);
this->goToBottom->setGeometry(0, this->height() - 32, this->width(), 32);
@@ -559,7 +559,7 @@ void ChannelView::drawMessages(QPainter &painter)
y += layout->getHeight();
end = layout;
if (y > height()) {
if (y > this->height()) {
break;
}
}
+39 -156
View File
@@ -4,11 +4,13 @@
#include <QListWidget>
#include <QPushButton>
#include <QTabWidget>
#include <QTableView>
#include <QTextEdit>
#include "debug/log.hpp"
#include "singletons/settingsmanager.hpp"
#include "util/layoutcreator.hpp"
#include "util/tupletablemodel.hpp"
#define ENABLE_HIGHLIGHTS "Enable Highlighting"
#define HIGHLIGHT_MSG "Highlight messages containing your name"
@@ -54,15 +56,47 @@ HighlightingPage::HighlightingPage()
// HIGHLIGHTS
auto highlights = tabs.appendTab(new QVBoxLayout, "Highlights");
{
highlights.emplace<QListWidget>().assign(&this->highlightList);
QTableView *view = *highlights.emplace<QTableView>();
auto *model = new util::TupleTableModel<QString, bool, bool, bool>;
model->setTitles({"Pattern", "Flash taskbar", "Play sound", "Regex"});
// fourtf: could crash
for (const messages::HighlightPhrase &phrase :
settings.highlightProperties.getValue()) {
model->addRow(phrase.key, phrase.alert, phrase.sound, phrase.regex);
}
view->setModel(model);
view->setSelectionMode(QAbstractItemView::SingleSelection);
view->setSelectionBehavior(QAbstractItemView::SelectRows);
view->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
view->resizeColumnsToContents();
view->setColumnWidth(0, 250);
auto buttons = highlights.emplace<QHBoxLayout>();
buttons.emplace<QPushButton>("Add").assign(&this->highlightAdd);
buttons.emplace<QPushButton>("Edit").assign(&this->highlightEdit);
buttons.emplace<QPushButton>("Remove").assign(&this->highlightRemove);
model->itemsChanged.connect([model] {
std::vector<messages::HighlightPhrase> phrases;
for (int i = 0; i < model->getRowCount(); i++) {
auto t = model->getRow(i);
phrases.push_back(messages::HighlightPhrase{
std::get<0>(t), std::get<1>(t), std::get<2>(t), std::get<3>(t),
});
}
singletons::SettingManager::getInstance().highlightProperties.setValue(phrases);
});
this->addHighlightTabSignals();
auto add = buttons.emplace<QPushButton>("Add");
QObject::connect(*add, &QPushButton::clicked,
[model] { model->addRow("", true, false, false); });
auto remove = buttons.emplace<QPushButton>("Remove");
QObject::connect(*remove, &QPushButton::clicked, [view, model] {
if (view->selectionModel()->hasSelection()) {
model->removeRow(view->selectionModel()->selectedRows()[0].row());
}
});
view->hideColumn(3);
}
// DISABLED USERS
auto disabledUsers = tabs.appendTab(new QVBoxLayout, "Disabled Users");
@@ -90,157 +124,6 @@ HighlightingPage::HighlightingPage()
this->disabledUsersChangedTimer.setSingleShot(true);
}
//
// DISCLAIMER:
//
// If you are trying to learn from reading the chatterino code please ignore this segment.
//
void HighlightingPage::addHighlightTabSignals()
{
singletons::SettingManager &settings = singletons::SettingManager::getInstance();
auto addBtn = this->highlightAdd;
auto editBtn = this->highlightEdit;
auto delBtn = this->highlightRemove;
auto highlights = this->highlightList;
// Open "Add new highlight" dialog
QObject::connect(addBtn, &QPushButton::clicked, this, [highlights, this, &settings] {
auto show = new QWidget();
auto box = new QBoxLayout(QBoxLayout::TopToBottom);
auto edit = new QLineEdit();
auto add = new QPushButton("Add");
auto sound = new QCheckBox("Play sound");
auto task = new QCheckBox("Flash taskbar");
// Save highlight
QObject::connect(add, &QPushButton::clicked, this, [=, &settings] {
if (edit->text().length()) {
QString highlightKey = edit->text();
highlights->addItem(highlightKey);
auto properties = settings.highlightProperties.getValue();
messages::HighlightPhrase newHighlightProperty;
newHighlightProperty.key = highlightKey;
newHighlightProperty.sound = sound->isChecked();
newHighlightProperty.alert = task->isChecked();
properties.push_back(newHighlightProperty);
settings.highlightProperties = properties;
show->close();
}
});
box->addWidget(edit);
box->addWidget(add);
box->addWidget(sound);
box->addWidget(task);
show->setLayout(box);
show->show();
});
// Open "Edit selected highlight" dialog
QObject::connect(editBtn, &QPushButton::clicked, this, [highlights, this, &settings] {
if (highlights->selectedItems().isEmpty()) {
// No item selected
return;
}
QListWidgetItem *selectedHighlight = highlights->selectedItems().first();
QString highlightKey = selectedHighlight->text();
auto properties = settings.highlightProperties.getValue();
auto highlightIt = std::find_if(properties.begin(), properties.end(),
[highlightKey](const auto &highlight) {
return highlight.key == highlightKey; //
});
if (highlightIt == properties.end()) {
debug::Log("Unable to find highlight key {} in highlight properties. "
"This is weird",
highlightKey);
return;
}
messages::HighlightPhrase &selectedSetting = *highlightIt;
auto show = new QWidget();
auto box = new QBoxLayout(QBoxLayout::TopToBottom);
auto edit = new QLineEdit(highlightKey);
auto apply = new QPushButton("Apply");
auto sound = new QCheckBox("Play sound");
sound->setChecked(selectedSetting.sound);
auto task = new QCheckBox("Flash taskbar");
task->setChecked(selectedSetting.alert);
// Apply edited changes
QObject::connect(apply, &QPushButton::clicked, this, [=, &settings] {
QString newHighlightKey = edit->text();
if (newHighlightKey.length() == 0) {
return;
}
auto properties = settings.highlightProperties.getValue();
auto highlightIt =
std::find_if(properties.begin(), properties.end(), [=](const auto &highlight) {
return highlight.key == highlightKey; //
});
if (highlightIt == properties.end()) {
debug::Log("Unable to find highlight key {} in highlight properties. "
"This is weird",
highlightKey);
return;
}
auto &highlightProperty = *highlightIt;
highlightProperty.key = newHighlightKey;
highlightProperty.sound = sound->isCheckable();
highlightProperty.alert = task->isCheckable();
settings.highlightProperties = properties;
selectedHighlight->setText(newHighlightKey);
selectedHighlight->setText(newHighlightKey);
show->close();
});
box->addWidget(edit);
box->addWidget(apply);
box->addWidget(sound);
box->addWidget(task);
show->setLayout(box);
show->show();
});
// Delete selected highlight
QObject::connect(delBtn, &QPushButton::clicked, this, [highlights, &settings] {
if (highlights->selectedItems().isEmpty()) {
// No highlight selected
return;
}
QListWidgetItem *selectedHighlight = highlights->selectedItems().first();
QString highlightKey = selectedHighlight->text();
auto properties = settings.highlightProperties.getValue();
properties.erase(std::remove_if(properties.begin(), properties.end(),
[highlightKey](const auto &highlight) {
return highlight.key == highlightKey; //
}),
properties.end());
settings.highlightProperties = properties;
delete selectedHighlight;
});
}
} // namespace settingspages
} // namespace widgets
} // namespace chatterino
@@ -2,6 +2,7 @@
#include "widgets/settingspages/settingspage.hpp"
#include <QAbstractTableModel>
#include <QTimer>
class QPushButton;
@@ -17,14 +18,7 @@ public:
HighlightingPage();
private:
QListWidget *highlightList;
QPushButton *highlightAdd;
QPushButton *highlightEdit;
QPushButton *highlightRemove;
QTimer disabledUsersChangedTimer;
void addHighlightTabSignals();
};
} // namespace settingspages
+36 -17
View File
@@ -37,16 +37,24 @@ namespace chatterino {
namespace widgets {
Split::Split(SplitContainer *parent)
: BaseWidget(parent)
, parentPage(*parent)
: Split((BaseWidget *)parent)
{
this->container = parent;
}
Split::Split(BaseWidget *widget)
: Split(widget->themeManager, widget)
{
}
Split::Split(singletons::ThemeManager &manager, QWidget *parent)
: BaseWidget(manager, parent)
, container(nullptr)
, channel(Channel::getEmpty())
, vbox(this)
, header(this)
, view(this)
, input(this)
, flexSizeX(1)
, flexSizeY(1)
, moderationMode(false)
{
this->setMouseTracking(true);
@@ -122,6 +130,11 @@ Split::~Split()
this->indirectChannelChangedConnection.disconnect();
}
bool Split::isInContainer() const
{
return this->container != nullptr;
}
IndirectChannel Split::getIndirectChannel()
{
return this->channel;
@@ -160,24 +173,26 @@ void Split::setChannel(IndirectChannel newChannel)
void Split::setFlexSizeX(double x)
{
this->flexSizeX = x;
this->parentPage.updateFlexValues();
// this->flexSizeX = x;
// this->parentPage->updateFlexValues();
}
double Split::getFlexSizeX()
{
return this->flexSizeX;
// return this->flexSizeX;
return 1;
}
void Split::setFlexSizeY(double y)
{
this->flexSizeY = y;
this->parentPage.updateFlexValues();
// this->flexSizeY = y;
// this->parentPage.updateFlexValues();
}
double Split::getFlexSizeY()
{
return this->flexSizeY;
// return this->flexSizeY;
return 1;
}
void Split::setModerationMode(bool value)
@@ -206,7 +221,9 @@ void Split::showChangeChannelPopup(const char *dialogTitle, bool empty,
dialog->closed.connect([=] {
if (dialog->hasSeletedChannel()) {
this->setChannel(dialog->getSelectedChannel());
this->parentPage.refreshTitle();
if (this->isInContainer()) {
this->container->refreshTitle();
}
}
callback(dialog->hasSeletedChannel());
@@ -286,15 +303,17 @@ void Split::handleModifiers(QEvent *event, Qt::KeyboardModifiers modifiers)
/// Slots
void Split::doAddSplit()
{
SplitContainer *page = static_cast<SplitContainer *>(this->parentWidget());
page->addChat(true);
if (this->container) {
this->container->addChat(true);
}
}
void Split::doCloseSplit()
{
SplitContainer *page = static_cast<SplitContainer *>(this->parentWidget());
page->removeFromLayout(this);
deleteLater();
if (this->container) {
this->container->removeFromLayout(this);
deleteLater();
}
}
void Split::doChangeChannel()
+10 -5
View File
@@ -43,7 +43,9 @@ class Split : public BaseWidget
Q_OBJECT
public:
Split(SplitContainer *parent);
explicit Split(SplitContainer *parent);
explicit Split(BaseWidget *widget);
explicit Split(singletons::ThemeManager &manager, QWidget *parent);
~Split() override;
pajlada::Signals::NoArgSignal channelChanged;
@@ -75,6 +77,8 @@ public:
void drag();
bool isInContainer() const;
protected:
void paintEvent(QPaintEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
@@ -83,21 +87,22 @@ protected:
void keyReleaseEvent(QKeyEvent *event) override;
private:
SplitContainer &parentPage;
SplitContainer *container;
IndirectChannel channel;
QVBoxLayout vbox;
SplitHeader header;
ChannelView view;
SplitInput input;
double flexSizeX;
double flexSizeY;
double flexSizeX = 1;
double flexSizeY = 1;
bool moderationMode;
bool moderationMode = false;
pajlada::Signals::Connection channelIDChangedConnection;
pajlada::Signals::Connection usermodeChangedConnection;
pajlada::Signals::Connection indirectChannelChangedConnection;
void doOpenAccountPopupWidget(AccountPopupWidget *widget, QString user);
void channelNameUpdated(const QString &newChannelName);
void handleModifiers(QEvent *event, Qt::KeyboardModifiers modifiers);
+4
View File
@@ -11,10 +11,14 @@
#include "widgets/split.hpp"
#include <QApplication>
#include <QHeaderView>
#include <QPalette>
#include <QShortcut>
#include <QVBoxLayout>
#include <QTableView>
#include "util/tupletablemodel.hpp"
namespace chatterino {
namespace widgets {
+1 -1
View File
@@ -23,7 +23,7 @@ class Window : public BaseWindow
Q_OBJECT
public:
enum WindowType { Main, Popup };
enum WindowType { Main, Popup, Attached };
explicit Window(singletons::ThemeManager &_themeManager, WindowType type);