File indexing completed on 2024-03-24 17:07:16
0001 /* 0002 * Copyright 2017-2018 Michail Vourlakos <mvourlakos@gmail.com> 0003 * 0004 * This file is part of Latte-Dock 0005 * 0006 * Latte-Dock is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU General Public License as 0008 * published by the Free Software Foundation; either version 2 of 0009 * the License, or (at your option) any later version. 0010 * 0011 * Latte-Dock is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "checkboxdelegate.h" 0021 0022 // local 0023 #include "../settingsdialog.h" 0024 #include "../tools/settingstools.h" 0025 0026 // Qt 0027 #include <QApplication> 0028 #include <QDebug> 0029 #include <QEvent> 0030 #include <QKeyEvent> 0031 #include <QMouseEvent> 0032 #include <QPainter> 0033 #include <QStandardItemModel> 0034 0035 const int HIDDENTEXTCOLUMN = 1; 0036 0037 CheckBoxDelegate::CheckBoxDelegate(QObject *parent) 0038 : QStyledItemDelegate(parent) 0039 { 0040 auto *settingsDialog = qobject_cast<Latte::SettingsDialog *>(parent); 0041 0042 if (settingsDialog) { 0043 m_settingsDialog = settingsDialog; 0044 } 0045 } 0046 0047 void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0048 { 0049 QStyleOptionViewItem adjustedOption = option; 0050 //! Remove the focus dotted lines 0051 adjustedOption.state = (adjustedOption.state & ~QStyle::State_HasFocus); 0052 0053 if (adjustedOption.state & QStyle::State_Enabled) { 0054 QStandardItemModel *model = (QStandardItemModel *) index.model(); 0055 QStyledItemDelegate::paint(painter, adjustedOption, model->index(index.row(), HIDDENTEXTCOLUMN)); 0056 0057 QStyledItemDelegate::paint(painter, adjustedOption, index); 0058 } else { 0059 // Disabled 0060 bool isSelected{Latte::isSelected(option)}; 0061 QPalette::ColorRole backColorRole = isSelected ? QPalette::Highlight : QPalette::Base; 0062 QPalette::ColorRole textColorRole = isSelected ? QPalette::HighlightedText : QPalette::Text; 0063 0064 // background 0065 painter->fillRect(option.rect, option.palette.brush(Latte::colorGroup(option), backColorRole)); 0066 0067 // text 0068 QPen pen(Qt::DashDotDotLine); 0069 pen.setWidth(2); pen.setColor(option.palette.brush(Latte::colorGroup(option), textColorRole).color()); 0070 int y = option.rect.y()+option.rect.height()/2; 0071 0072 bool inMenu = m_settingsDialog->isMenuCell(index.column()); 0073 int space = inMenu ? option.rect.height() / 2 : 0; 0074 0075 painter->setPen(pen); 0076 0077 if (qApp->layoutDirection() == Qt::LeftToRight) { 0078 painter->drawLine(option.rect.x() + space, y, 0079 option.rect.x() + option.rect.width(), y); 0080 0081 if (inMenu) { 0082 int xm = option.rect.x() + space; 0083 int thick = option.rect.height() / 2; 0084 int ym = option.rect.y() + ((option.rect.height() - thick) / 2); 0085 0086 pen.setStyle(Qt::SolidLine); 0087 painter->setPen(pen); 0088 painter->drawLine(xm, ym, xm, ym + thick); 0089 } 0090 0091 } else { 0092 painter->drawLine(option.rect.x(), y, 0093 option.rect.x()+option.rect.width() - space, y); 0094 0095 if (inMenu) { 0096 int xm = option.rect.x() + option.rect.width() - space; 0097 int thick = option.rect.height() / 2; 0098 int ym = option.rect.y() + ((option.rect.height() - thick) / 2); 0099 0100 pen.setStyle(Qt::SolidLine); 0101 painter->setPen(pen); 0102 painter->drawLine(xm, ym, xm, ym + thick); 0103 } 0104 } 0105 } 0106 } 0107 0108 bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, 0109 const QModelIndex &index) 0110 { 0111 Q_ASSERT(event); 0112 Q_ASSERT(model); 0113 0114 // // make sure that the item is checkable 0115 // Qt::ItemFlags flags = model->flags(index); 0116 // 0117 // if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled)) 0118 // return false; 0119 // 0120 // // make sure that we have a check state 0121 QString value{index.data().toString()}; 0122 // 0123 // if (!value.isValid()) 0124 // return false; 0125 0126 // make sure that we have the right event type 0127 if (event->type() == QEvent::MouseButtonDblClick) { 0128 if (!option.rect.contains(static_cast<QMouseEvent *>(event)->pos())) 0129 return false; 0130 } else if (event->type() == QEvent::KeyPress) { 0131 if (static_cast<QKeyEvent *>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent *>(event)->key() != Qt::Key_Select) 0132 return false; 0133 } else { 0134 return false; 0135 } 0136 0137 const QChar CheckMark{0x2714}; 0138 return model->setData(index, value == CheckMark ? QString("") : CheckMark, Qt::DisplayRole); 0139 }