| 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 "ktableview.h" |
| 24 |
#include "ktableheaderview.h" |
| 25 |
#include "themeController.h" |
| 26 |
#include <QApplication> |
| 27 |
#include <QCheckBox> |
| 28 |
#include <QDebug> |
| 29 |
#include <QHeaderView> |
| 30 |
#include <QItemSelectionModel> |
| 31 |
#include <QMouseEvent> |
| 32 |
#include <QPainter> |
| 33 |
#include <QStyledItemDelegate> |
| 34 |
#include <QToolButton> |
| 35 |
|
| 36 |
namespace kdk |
| 37 |
{ |
| 38 |
|
| 39 |
class Q_DECL_HIDDEN TableDelegate : public QStyledItemDelegate |
| 40 |
{ |
| 41 |
Q_OBJECT |
| 42 |
public: |
| 43 |
TableDelegate(QObject *parent); |
| 44 |
|
| 45 |
void updateState(Qt::CheckState state, const QModelIndex &index); |
| 46 |
|
| 47 |
QList<int> selectList(); |
| 48 |
|
| 49 |
protected: |
| 50 |
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; |
| 51 |
|
| 52 |
virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override; |
| 53 |
|
| 54 |
Q_SIGNALS: |
| 55 |
void checkChange(int state); |
| 56 |
|
| 57 |
public slots: |
| 58 |
void checkChangeSlot(int state); |
| 59 |
void onHoverIndexChanged(QModelIndex index); |
| 60 |
// void selectChange(int row); |
| 61 |
|
| 62 |
private: |
| 63 |
QList<int> m_checkList; |
| 64 |
int m_hoverRow; |
| 65 |
}; |
| 66 |
|
| 67 |
class Q_DECL_HIDDEN KTableViewPrivate : public QObject, public ThemeController |
| 68 |
{ |
| 69 |
Q_OBJECT |
| 70 |
Q_DECLARE_PUBLIC(KTableView) |
| 71 |
public: |
| 72 |
KTableViewPrivate(KTableView *parent); |
| 73 |
virtual ~KTableViewPrivate(); |
| 74 |
|
| 75 |
void changeTheme(); |
| 76 |
|
| 77 |
private: |
| 78 |
KTableView *q_ptr; |
| 79 |
KTableHeaderView *m_pHeaderView; |
| 80 |
TableDelegate *m_pDelegate; |
| 81 |
}; |
| 82 |
|
| 83 |
KTableView::KTableView(QWidget *parent) |
| 84 |
: QTableView(parent) |
| 85 |
, d_ptr(new KTableViewPrivate(this)) |
| 86 |
{ |
| 87 |
Q_D(KTableView); |
| 88 |
setShowGrid(false); |
| 89 |
verticalHeader()->setVisible(false); |
| 90 |
setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 91 |
setSelectionBehavior(QAbstractItemView::SelectRows); |
| 92 |
setMouseTracking(true); |
| 93 |
installEventFilter(this); |
| 94 |
|
| 95 |
d->m_pHeaderView = new KTableHeaderView(Qt::Horizontal, this); |
| 96 |
d->m_pHeaderView->installEventFilter(this); |
| 97 |
|
| 98 |
d->m_pDelegate = new TableDelegate(this); |
| 99 |
setItemDelegate(d->m_pDelegate); |
| 100 |
|
| 101 |
connect(d->m_pDelegate, &TableDelegate::checkChange, d->m_pHeaderView, [=](int state) { |
| 102 |
d->m_pHeaderView->checkStateChangeSlot(state); |
| 103 |
}); |
| 104 |
connect(d->m_pHeaderView, &KTableHeaderView::checkStateChange, d->m_pDelegate, [=](int state) { |
| 105 |
d->m_pDelegate->checkChangeSlot(state); |
| 106 |
QItemSelectionModel *SelectionModel = selectionModel(); |
| 107 |
if (SelectionModel) { |
| 108 |
SelectionModel->clearSelection(); |
| 109 |
} |
| 110 |
emit hoverIndexChanged(QModelIndex()); |
| 111 |
}); |
| 112 |
|
| 113 |
connect(this, SIGNAL(hoverIndexChanged(QModelIndex)), d->m_pDelegate, SLOT(onHoverIndexChanged(QModelIndex))); |
| 114 |
connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 115 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 116 |
{ |
| 117 |
d->changeTheme(); |
| 118 |
} |
| 119 |
}); |
| 120 |
} |
| 121 |
|
| 122 |
void KTableView::setHorizontalTitle(QStringList list) |
| 123 |
{ |
| 124 |
Q_D(KTableView); |
| 125 |
d->m_pHeaderView->setTitle(list); |
| 126 |
setHorizontalHeader(d->m_pHeaderView); |
| 127 |
} |
| 128 |
|
| 129 |
KTableHeaderView *KTableView::headerView() |
| 130 |
{ |
| 131 |
Q_D(KTableView); |
| 132 |
if (d->m_pHeaderView) |
| 133 |
return d->m_pHeaderView; |
| 134 |
} |
| 135 |
|
| 136 |
QList<int> KTableView::selectList() |
| 137 |
{ |
| 138 |
Q_D(KTableView); |
| 139 |
return d->m_pDelegate->selectList(); |
| 140 |
} |
| 141 |
|
| 142 |
void KTableView::mouseMoveEvent(QMouseEvent *event) |
| 143 |
{ |
| 144 |
QModelIndex index = indexAt(event->pos()); |
| 145 |
emit hoverIndexChanged(index); |
| 146 |
QTableView::mouseMoveEvent(event); |
| 147 |
this->viewport()->update(); |
| 148 |
} |
| 149 |
|
| 150 |
bool KTableView::eventFilter(QObject *object, QEvent *event) |
| 151 |
{ |
| 152 |
Q_D(KTableView); |
| 153 |
if (object == this) { |
| 154 |
switch (event->type()) { |
| 155 |
case QEvent::Leave: |
| 156 |
emit hoverIndexChanged(QModelIndex()); |
| 157 |
break; |
| 158 |
default: |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
if (object == d->m_pHeaderView) { |
| 163 |
switch (event->type()) { |
| 164 |
case QEvent::Enter: |
| 165 |
emit hoverIndexChanged(QModelIndex()); |
| 166 |
break; |
| 167 |
default: |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
this->viewport()->update(); |
| 172 |
return QTableView::eventFilter(object, event); |
| 173 |
} |
| 174 |
|
| 175 |
KTableViewPrivate::KTableViewPrivate(KTableView *parent) |
| 176 |
: q_ptr(parent) |
| 177 |
{ |
| 178 |
} |
| 179 |
|
| 180 |
KTableViewPrivate::~KTableViewPrivate() |
| 181 |
{ |
| 182 |
} |
| 183 |
|
| 184 |
void KTableViewPrivate::changeTheme() |
| 185 |
{ |
| 186 |
initThemeStyle(); |
| 187 |
} |
| 188 |
|
| 189 |
TableDelegate::TableDelegate(QObject *parent) |
| 190 |
: QStyledItemDelegate(parent) |
| 191 |
{ |
| 192 |
m_hoverRow = -1; |
| 193 |
} |
| 194 |
|
| 195 |
void TableDelegate::updateState(Qt::CheckState state, const QModelIndex &index) |
| 196 |
{ |
| 197 |
if (state == Qt::Checked) { |
| 198 |
m_checkList.append(index.row()); |
| 199 |
} else if (state == Qt::Unchecked) { |
| 200 |
|
| 201 |
m_checkList.removeOne(index.row()); |
| 202 |
} |
| 203 |
QAbstractItemView *view = qobject_cast<QAbstractItemView *>(parent()); |
| 204 |
int count = view->model()->rowCount(); |
| 205 |
|
| 206 |
if (m_checkList.count() == 0) |
| 207 |
emit checkChange(Qt::Unchecked); |
| 208 |
else if (m_checkList.count() == count) |
| 209 |
emit checkChange(Qt::Checked); |
| 210 |
else |
| 211 |
emit checkChange(Qt::PartiallyChecked); |
| 212 |
} |
| 213 |
|
| 214 |
void TableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 215 |
{ |
| 216 |
QPalette pale; |
| 217 |
QColor bkgColor; |
| 218 |
QColor hoverColor; |
| 219 |
|
| 220 |
if (ThemeController::themeMode() == LightTheme) { |
| 221 |
if (index.row() % 2 == 0) |
| 222 |
bkgColor = QColor(245, 245, 245); |
| 223 |
else |
| 224 |
bkgColor = ThemeController::getCustomColorFromDT("kwhite"); |
| 225 |
hoverColor = ThemeController::getCustomColorFromDT("kblack"); |
| 226 |
hoverColor.setAlphaF(0.05); |
| 227 |
} else { |
| 228 |
if (index.row() % 2 == 0) |
| 229 |
bkgColor = ThemeController::getCustomColorFromDT("kgray-17"); |
| 230 |
else |
| 231 |
bkgColor = QColor(18, 18, 18); |
| 232 |
hoverColor = ThemeController::getCustomColorFromDT("kwhite"); |
| 233 |
hoverColor.setAlphaF(0.10); |
| 234 |
} |
| 235 |
if (index.row() == m_hoverRow && !option.state.testFlag(QStyle::State_Selected)) { |
| 236 |
painter->fillRect(option.rect, hoverColor); |
| 237 |
} else { |
| 238 |
if (option.state.testFlag(QStyle::State_Selected)) |
| 239 |
painter->fillRect(option.rect, ThemeController::getCustomColorFromDT("highlight-active")); |
| 240 |
else { |
| 241 |
painter->fillRect(option.rect, bkgColor); |
| 242 |
} |
| 243 |
painter->setPen(Qt::NoPen); |
| 244 |
painter->drawRect(option.rect); |
| 245 |
} |
| 246 |
|
| 247 |
painter->setPen(ThemeController::getCustomColorFromDT("windowtext-active")); |
| 248 |
if (index.column() == 0) { |
| 249 |
QRect rect = option.rect; |
| 250 |
// 绘制复选框 |
| 251 |
QStyleOptionButton checkBoxStyle; |
| 252 |
checkBoxStyle.rect = QRect(rect.x() + 8, rect.y() + (rect.height() - 16) / 2, 16, 16); |
| 253 |
|
| 254 |
Qt::CheckState checkState = static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt()); |
| 255 |
checkBoxStyle.state = (checkState == Qt::Checked) ? QStyle::State_On : QStyle::State_Off; |
| 256 |
checkBoxStyle.state |= QStyle::State_Enabled; |
| 257 |
|
| 258 |
QApplication::style()->drawPrimitive(QStyle::PE_IndicatorItemViewItemCheck, &checkBoxStyle, painter, option.widget); |
| 259 |
|
| 260 |
painter->drawText(option.rect.adjusted(checkBoxStyle.rect.right() + 10, 0, 0, 0), index.data(Qt::DisplayRole).toString()); |
| 261 |
} else { |
| 262 |
painter->drawText(option.rect, index.data(Qt::DisplayRole).toString()); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
bool TableDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) |
| 267 |
{ |
| 268 |
if ((event->type() == QEvent::MouseButtonRelease) && (index.column() == 0)) { |
| 269 |
QMouseEvent *mouse_event = static_cast<QMouseEvent *>(event); |
| 270 |
if (mouse_event->button() == Qt::LeftButton) { |
| 271 |
QRect r(option.rect.left() + 8, option.rect.y() + (option.rect.height() - 16) / 2, 16, 16); |
| 272 |
bool flag = r.contains(mouse_event->pos()); |
| 273 |
if (flag) { |
| 274 |
// 根据当前单元格的选中状态来在 选中/未选中 之间切换 |
| 275 |
QVariant value = index.data(Qt::CheckStateRole); |
| 276 |
Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked |
| 277 |
? Qt::Unchecked |
| 278 |
: Qt::Checked); |
| 279 |
|
| 280 |
// 设置当前单元格的选中状态 |
| 281 |
bool checkState = model->setData(index, state, Qt::CheckStateRole); |
| 282 |
updateState(state, index); |
| 283 |
return checkState; |
| 284 |
} |
| 285 |
} |
| 286 |
return false; |
| 287 |
} else |
| 288 |
return QStyledItemDelegate::editorEvent(event, model, option, index); |
| 289 |
} |
| 290 |
|
| 291 |
QList<int> TableDelegate::selectList() |
| 292 |
{ |
| 293 |
return m_checkList; |
| 294 |
} |
| 295 |
|
| 296 |
void TableDelegate::checkChangeSlot(int state) |
| 297 |
{ |
| 298 |
m_checkList.clear(); |
| 299 |
QAbstractItemView *view = qobject_cast<QAbstractItemView *>(parent()); |
| 300 |
int count = view->model()->rowCount(); |
| 301 |
Qt::CheckState checkState; |
| 302 |
|
| 303 |
for (int i = 0; i < count; i++) { |
| 304 |
if (state == 0) { |
| 305 |
m_checkList.append(view->model()->index(i, 0).row()); |
| 306 |
checkState = Qt::Unchecked; |
| 307 |
} else { |
| 308 |
m_checkList.removeOne(view->model()->index(i, 0).row()); |
| 309 |
checkState = Qt::Checked; |
| 310 |
} |
| 311 |
// 设置当前单元格的选中状态 |
| 312 |
view->model()->setData(view->model()->index(i, 0), checkState, Qt::CheckStateRole); |
| 313 |
updateState(checkState, view->model()->index(i, 0)); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
void TableDelegate::onHoverIndexChanged(QModelIndex index) |
| 318 |
{ |
| 319 |
if (index.isValid()) { |
| 320 |
m_hoverRow = index.row(); |
| 321 |
} else { |
| 322 |
m_hoverRow = -1; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
#include "ktableview.moc" |
| 329 |
#include "moc_ktableview.cpp" |