1
0
mirror of https://github.com/KDE/latte-dock.git synced 2024-12-26 23:21:37 +03:00

center checkboxes in layouts window

This commit is contained in:
Michail Vourlakos 2017-07-18 03:27:35 +03:00
parent 049e539c99
commit 86bc896fa0
4 changed files with 76 additions and 0 deletions

View File

@ -20,6 +20,7 @@ set(lattedock-app_SRCS
layoutsettings.cpp
layoutconfigdialog.cpp
importer.cpp
layoutsDelegates/checkboxdelegate.cpp
main.cpp
)

View File

@ -22,6 +22,7 @@
#include "ui_layoutconfigdialog.h"
#include "layoutconfigdialog.h"
#include "layoutsettings.h"
#include "layoutsDelegates/checkboxdelegate.h"
#include <QDir>
#include <QStandardItem>
@ -64,6 +65,8 @@ LayoutConfigDialog::LayoutConfigDialog(QWidget *parent, LayoutManager *manager)
connect(m_manager, &LayoutManager::currentLayoutNameChanged, this, &LayoutConfigDialog::currentLayoutNameChanged);
loadLayouts();
ui->layoutsView->setItemDelegateForColumn(3, new CheckBoxDelegate(this));
}
LayoutConfigDialog::~LayoutConfigDialog()

View File

@ -0,0 +1,63 @@
#include "checkboxdelegate.h"
#include <QApplication>
#include <QEvent>
#include <QKeyEvent>
#include <QMouseEvent>
CheckBoxDelegate::CheckBoxDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem viewItemOption(option);
QRect newRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
QSize(option.decorationSize.width(), option.decorationSize.height()),
QRect(option.rect.x(), option.rect.y(),
option.rect.width(), option.rect.height()));
viewItemOption.rect = newRect;
QStyledItemDelegate::paint(painter, viewItemOption, index);
}
bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
const QModelIndex &index)
{
Q_ASSERT(event);
Q_ASSERT(model);
// make sure that the item is checkable
Qt::ItemFlags flags = model->flags(index);
if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled))
return false;
// make sure that we have a check state
QVariant value = index.data(Qt::CheckStateRole);
if (!value.isValid())
return false;
// make sure that we have the right event type
if (event->type() == QEvent::MouseButtonRelease) {
QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
option.decorationSize,
QRect(option.rect.x(), option.rect.y(),
option.rect.width(), option.rect.height()));
if (!checkRect.contains(static_cast<QMouseEvent *>(event)->pos()))
return false;
} else if (event->type() == QEvent::KeyPress) {
if (static_cast<QKeyEvent *>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent *>(event)->key() != Qt::Key_Select)
return false;
} else {
return false;
}
Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
? Qt::Unchecked : Qt::Checked);
return model->setData(index, state, Qt::CheckStateRole);
}

View File

@ -0,0 +1,9 @@
#include <QStyledItemDelegate>
class CheckBoxDelegate : public QStyledItemDelegate {
public:
CheckBoxDelegate(QObject *parent = 0);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
};