| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhenyu Wang <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "ktableheaderview.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QApplication> |
| 26 |
#include <QCheckBox> |
| 27 |
#include <QDebug> |
| 28 |
#include <QEvent> |
| 29 |
#include <QMap> |
| 30 |
#include <QMenu> |
| 31 |
#include <QMouseEvent> |
| 32 |
#include <QPainter> |
| 33 |
#include <QToolButton> |
| 34 |
|
| 35 |
namespace kdk |
| 36 |
{ |
| 37 |
|
| 38 |
class Q_DECL_HIDDEN KTableHeaderViewPrivate : public QObject, public ThemeController |
| 39 |
{ |
| 40 |
Q_OBJECT |
| 41 |
Q_DECLARE_PUBLIC(KTableHeaderView) |
| 42 |
public: |
| 43 |
KTableHeaderViewPrivate(KTableHeaderView *PARENT); |
| 44 |
|
| 45 |
void changeTheme(); |
| 46 |
|
| 47 |
private: |
| 48 |
KTableHeaderView *q_ptr; |
| 49 |
QStringList m_HeaderList; |
| 50 |
bool m_bChecked; |
| 51 |
bool m_bTristate; |
| 52 |
bool m_bNoChange; |
| 53 |
bool m_bPressed; |
| 54 |
QMap<int, QMenu *> m_map; |
| 55 |
}; |
| 56 |
|
| 57 |
KTableHeaderView::KTableHeaderView(Qt::Orientation orientation, QWidget *parent) |
| 58 |
: QHeaderView(orientation, parent) |
| 59 |
, d_ptr(new KTableHeaderViewPrivate(this)) |
| 60 |
|
| 61 |
{ |
| 62 |
Q_D(KTableHeaderView); |
| 63 |
// 设置Section可点击,若不设置则不能发出sectionClicked信号 |
| 64 |
this->setSectionsClickable(true); |
| 65 |
connect(this, &KTableHeaderView::clickedHeader, this, [=](int column) { |
| 66 |
if (d->m_map.contains(column)) { |
| 67 |
d->m_map.value(column)->exec(QCursor::pos()); |
| 68 |
} |
| 69 |
}); |
| 70 |
} |
| 71 |
|
| 72 |
void KTableHeaderView::setTitle(QStringList list) |
| 73 |
{ |
| 74 |
Q_D(KTableHeaderView); |
| 75 |
d->m_HeaderList = list; |
| 76 |
} |
| 77 |
|
| 78 |
void KTableHeaderView::setMenu(QMenu *menu, int column) |
| 79 |
{ |
| 80 |
Q_D(KTableHeaderView); |
| 81 |
d->m_map.insert(column, menu); |
| 82 |
} |
| 83 |
|
| 84 |
void KTableHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const |
| 85 |
{ |
| 86 |
Q_D(const KTableHeaderView); |
| 87 |
painter->save(); |
| 88 |
QHeaderView::paintSection(painter, rect, logicalIndex); |
| 89 |
painter->restore(); |
| 90 |
|
| 91 |
if (logicalIndex == 0) { |
| 92 |
QStyleOptionButton option; |
| 93 |
|
| 94 |
option.initFrom(this); |
| 95 |
|
| 96 |
if (d->m_bTristate && d->m_bNoChange) { |
| 97 |
option.state |= QStyle::State_NoChange; |
| 98 |
} else |
| 99 |
option.state |= d->m_bChecked ? QStyle::State_On : QStyle::State_Off; |
| 100 |
|
| 101 |
option.iconSize = QSize(16, 16); |
| 102 |
option.rect = QRect(rect.x() + 8, rect.y() + (rect.height() - 16) / 2, 16, 16); |
| 103 |
style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter, this); |
| 104 |
|
| 105 |
} else { |
| 106 |
if (d->m_map.contains(logicalIndex)) { |
| 107 |
QPixmap pixmap = QIcon::fromTheme("ukui-down-symbolic").pixmap(16, 16); |
| 108 |
if (ThemeController::themeMode() == DarkTheme) |
| 109 |
pixmap = ThemeController::drawSymbolicColoredPixmap(pixmap); |
| 110 |
QRect iconRect(rect.right() - 24, rect.height() / 2 - 8, 16, 16); |
| 111 |
painter->drawPixmap(iconRect, pixmap); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
QRect textRect; |
| 116 |
if (logicalIndex > -1 || logicalIndex < d->m_HeaderList.size()) { |
| 117 |
if (logicalIndex == 0) { |
| 118 |
textRect = rect.adjusted(32, 0, 0, 0); |
| 119 |
if (fontMetrics().horizontalAdvance(d->m_HeaderList.at(logicalIndex)) > textRect.width()) { |
| 120 |
QString str = fontMetrics().elidedText(d->m_HeaderList.at(logicalIndex), Qt::ElideRight, textRect.width()); |
| 121 |
painter->drawText(textRect, Qt::AlignVCenter, str); |
| 122 |
} else |
| 123 |
painter->drawText(textRect, Qt::AlignVCenter, d->m_HeaderList.at(logicalIndex)); |
| 124 |
} else { |
| 125 |
if (d->m_map.contains(logicalIndex)) |
| 126 |
textRect = rect.adjusted(6, 0, -24, 0); |
| 127 |
else |
| 128 |
textRect = rect.adjusted(6, 0, -6, 0); |
| 129 |
if (fontMetrics().horizontalAdvance(d->m_HeaderList.at(logicalIndex)) > textRect.width()) { |
| 130 |
QString str = fontMetrics().elidedText(d->m_HeaderList.at(logicalIndex), Qt::ElideRight, textRect.width()); |
| 131 |
painter->drawText(textRect, Qt::AlignVCenter, str); |
| 132 |
} else |
| 133 |
painter->drawText(textRect, Qt::AlignVCenter, d->m_HeaderList.at(logicalIndex)); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void KTableHeaderView::mousePressEvent(QMouseEvent *e) |
| 139 |
{ |
| 140 |
Q_D(KTableHeaderView); |
| 141 |
int nColumn = logicalIndexAt(e->pos()); |
| 142 |
|
| 143 |
if ((e->buttons() & Qt::LeftButton)) { |
| 144 |
if (nColumn == 0) |
| 145 |
d->m_bPressed = true; |
| 146 |
else { |
| 147 |
// 每个标题栏的rect |
| 148 |
QRect tabRect(sectionPosition(nColumn), 0, sectionSize(nColumn), viewport()->rect().height()); |
| 149 |
// 是否在图标区域被点击 |
| 150 |
QRect sectionRect(tabRect.right() - 24, tabRect.y() + (tabRect.height() - 16) / 2, 16, 16); |
| 151 |
if (sectionRect.contains(e->pos()) && d->m_map.contains(nColumn)) |
| 152 |
emit clickedHeader(nColumn); |
| 153 |
QHeaderView::mousePressEvent(e); |
| 154 |
} |
| 155 |
} else { |
| 156 |
QHeaderView::mousePressEvent(e); |
| 157 |
} |
| 158 |
viewport()->update(); |
| 159 |
} |
| 160 |
|
| 161 |
void KTableHeaderView::mouseReleaseEvent(QMouseEvent *e) |
| 162 |
{ |
| 163 |
Q_D(KTableHeaderView); |
| 164 |
if (d->m_bPressed) { |
| 165 |
if (d->m_bTristate && d->m_bNoChange) { |
| 166 |
d->m_bChecked = true; |
| 167 |
d->m_bNoChange = false; |
| 168 |
} else { |
| 169 |
d->m_bChecked = !d->m_bChecked; |
| 170 |
} |
| 171 |
} else { |
| 172 |
QHeaderView::mouseReleaseEvent(e); |
| 173 |
} |
| 174 |
this->viewport()->update(); |
| 175 |
d->m_bPressed = false; |
| 176 |
if (d->m_bChecked) |
| 177 |
emit checkStateChange(Qt::Checked); |
| 178 |
else |
| 179 |
emit checkStateChange(Qt::Unchecked); |
| 180 |
} |
| 181 |
|
| 182 |
bool KTableHeaderView::event(QEvent *e) |
| 183 |
{ |
| 184 |
this->viewport()->update(); |
| 185 |
return QHeaderView::event(e); |
| 186 |
} |
| 187 |
|
| 188 |
void KTableHeaderView::checkStateChangeSlot(int iState) |
| 189 |
{ |
| 190 |
Q_D(KTableHeaderView); |
| 191 |
if (iState == Qt::PartiallyChecked) { |
| 192 |
d->m_bTristate = true; |
| 193 |
d->m_bNoChange = true; |
| 194 |
} else { |
| 195 |
d->m_bNoChange = false; |
| 196 |
} |
| 197 |
|
| 198 |
d->m_bChecked = (iState != Qt::Unchecked); |
| 199 |
this->viewport()->update(); |
| 200 |
} |
| 201 |
|
| 202 |
KTableHeaderViewPrivate::KTableHeaderViewPrivate(KTableHeaderView *parent) |
| 203 |
: q_ptr(parent) |
| 204 |
, m_bTristate(false) |
| 205 |
, m_bNoChange(false) |
| 206 |
, m_bChecked(false) |
| 207 |
, m_bPressed(false) |
| 208 |
{ |
| 209 |
} |
| 210 |
|
| 211 |
void KTableHeaderViewPrivate::changeTheme() |
| 212 |
{ |
| 213 |
Q_Q(KTableHeaderView); |
| 214 |
initThemeStyle(); |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
#include "ktableheaderview.moc" |
| 220 |
#include "moc_ktableheaderview.cpp" |