reworked commands settings page

This commit is contained in:
fourtf
2018-04-27 01:11:09 +02:00
parent e23ce31e05
commit 49069beed7
11 changed files with 221 additions and 50 deletions
@@ -0,0 +1,57 @@
#include "comboboxitemdelegate.hpp"
#include <QComboBox>
namespace chatterino {
namespace widgets {
ComboBoxItemDelegate::ComboBoxItemDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
ComboBoxItemDelegate::~ComboBoxItemDelegate()
{
}
QWidget *ComboBoxItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QVariant data = index.data(Qt::UserRole + 1);
if (data.type() != QVariant::StringList) {
return QStyledItemDelegate::createEditor(parent, option, index);
}
QComboBox *combo = new QComboBox(parent);
combo->addItems(data.toStringList());
return combo;
}
void ComboBoxItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (QComboBox *cb = qobject_cast<QComboBox *>(editor)) {
// get the index of the text in the combobox that matches the current value of the itenm
QString currentText = index.data(Qt::EditRole).toString();
int cbIndex = cb->findText(currentText);
// if it is valid, adjust the combobox
if (cbIndex >= 0) {
cb->setCurrentIndex(cbIndex);
}
} else {
QStyledItemDelegate::setEditorData(editor, index);
}
}
void ComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
if (QComboBox *cb = qobject_cast<QComboBox *>(editor))
// save the current text of the combo box as the current value of the item
model->setData(index, cb->currentText(), Qt::EditRole);
else
QStyledItemDelegate::setModelData(editor, model, index);
}
} // namespace widgets
} // namespace chatterino
@@ -0,0 +1,26 @@
#pragma once
#include <QStyledItemDelegate>
namespace chatterino {
namespace widgets {
// stolen from https://wiki.qt.io/Combo_Boxes_in_Item_Views
class ComboBoxItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
ComboBoxItemDelegate(QObject *parent = 0);
~ComboBoxItemDelegate();
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;
virtual void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
};
} // namespace widgets
} // namespace chatterino
+12 -12
View File
@@ -27,7 +27,7 @@ NotebookTab2::NotebookTab2(Notebook2 *_notebook)
this->positionChangedAnimation.setEasingCurve(QEasingCurve(QEasingCurve::InCubic));
singletons::SettingManager::getInstance().hideTabX.connect(
singletons::SettingManager::getInstance().showTabCloseButton.connect(
boost::bind(&NotebookTab2::hideTabXChanged, this, _1), this->managedConnections);
this->setMouseTracking(true);
@@ -82,7 +82,7 @@ void NotebookTab2::updateSize()
int width;
QFontMetrics metrics(this->font());
if (singletons::SettingManager::getInstance().hideTabX) {
if (!singletons::SettingManager::getInstance().showTabCloseButton) {
width = (int)((metrics.width(this->title) + 16 /*+ 16*/) * scale);
} else {
width = (int)((metrics.width(this->title) + 8 + 24 /*+ 16*/) * scale);
@@ -248,7 +248,7 @@ void NotebookTab2::paintEvent(QPaintEvent *)
painter.setPen(colors.text);
// set area for text
int rectW = (settingManager.hideTabX ? 0 : static_cast<int>(16) * scale);
int rectW = (!settingManager.showTabCloseButton ? 0 : static_cast<int>(16) * scale);
QRect rect(0, 0, this->width() - rectW, height);
// draw text
@@ -269,7 +269,7 @@ void NotebookTab2::paintEvent(QPaintEvent *)
}
// draw close x
if (!settingManager.hideTabX && (mouseOver || selected)) {
if (settingManager.showTabCloseButton && (mouseOver || selected)) {
QRect xRect = this->getXRect();
if (!xRect.isNull()) {
if (mouseOverX) {
@@ -315,7 +315,7 @@ void NotebookTab2::mouseReleaseEvent(QMouseEvent *event)
this->notebook->removePage(this->page);
}
} else {
if (!singletons::SettingManager::getInstance().hideTabX && this->mouseDownX &&
if (singletons::SettingManager::getInstance().showTabCloseButton && this->mouseDownX &&
this->getXRect().contains(event->pos())) {
this->mouseDownX = false;
@@ -350,7 +350,7 @@ void NotebookTab2::dragEnterEvent(QDragEnterEvent *)
void NotebookTab2::mouseMoveEvent(QMouseEvent *event)
{
if (!singletons::SettingManager::getInstance().hideTabX &&
if (singletons::SettingManager::getInstance().showTabCloseButton &&
this->notebook->getAllowUserTabManagement()) //
{
bool overX = this->getXRect().contains(event->pos());
@@ -401,7 +401,7 @@ NotebookTab::NotebookTab(Notebook *_notebook)
this->positionChangedAnimation.setEasingCurve(QEasingCurve(QEasingCurve::InCubic));
singletons::SettingManager::getInstance().hideTabX.connect(
singletons::SettingManager::getInstance().showTabCloseButton.connect(
boost::bind(&NotebookTab::hideTabXChanged, this, _1), this->managedConnections);
this->setMouseTracking(true);
@@ -453,7 +453,7 @@ void NotebookTab::updateSize()
int width;
if (singletons::SettingManager::getInstance().hideTabX) {
if (!singletons::SettingManager::getInstance().showTabCloseButton) {
width = (int)((fontMetrics().width(this->title) + 16 /*+ 16*/) * scale);
} else {
width = (int)((fontMetrics().width(this->title) + 8 + 24 /*+ 16*/) * scale);
@@ -619,7 +619,7 @@ void NotebookTab::paintEvent(QPaintEvent *)
painter.setPen(colors.text);
// set area for text
int rectW = (settingManager.hideTabX ? 0 : static_cast<int>(16) * scale);
int rectW = (!settingManager.showTabCloseButton ? 0 : static_cast<int>(16) * scale);
QRect rect(0, 0, this->width() - rectW, height);
// draw text
@@ -640,7 +640,7 @@ void NotebookTab::paintEvent(QPaintEvent *)
}
// draw close x
if (!settingManager.hideTabX && (mouseOver || selected)) {
if (settingManager.showTabCloseButton && (mouseOver || selected)) {
QRect xRect = this->getXRect();
if (mouseOverX) {
painter.fillRect(xRect, QColor(0, 0, 0, 64));
@@ -682,7 +682,7 @@ void NotebookTab::mouseReleaseEvent(QMouseEvent *event)
this->notebook->removePage(this->page);
}
} else {
if (!singletons::SettingManager::getInstance().hideTabX && this->mouseDownX &&
if (singletons::SettingManager::getInstance().showTabCloseButton && this->mouseDownX &&
this->getXRect().contains(event->pos())) {
this->mouseDownX = false;
@@ -715,7 +715,7 @@ void NotebookTab::dragEnterEvent(QDragEnterEvent *)
void NotebookTab::mouseMoveEvent(QMouseEvent *event)
{
if (!singletons::SettingManager::getInstance().hideTabX) {
if (singletons::SettingManager::getInstance().showTabCloseButton) {
bool overX = this->getXRect().contains(event->pos());
if (overX != this->mouseOverX) {