File indexing completed on 2024-04-28 05:27:04

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2000-2008 David Faure <faure@kde.org>
0003     SPDX-FileCopyrightText: 2008 Urs Wolfer <uwolfer @ kde.org>
0004     SPDX-FileCopyrightText: 2022 Marco Rebhan <me@dblsaiko.net>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "typeslisttreewidget.h"
0010 
0011 #include "typeslistitembase.h"
0012 #include <QKeyEvent>
0013 
0014 TypesListTreeWidget::TypesListTreeWidget(QWidget *parent)
0015     : QTreeWidget(parent)
0016 {
0017 }
0018 
0019 void TypesListTreeWidget::drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0020 {
0021     static_cast<TypesListItemBase *>(itemFromIndex(index))->loadIcon();
0022 
0023     QTreeWidget::drawRow(painter, option, index);
0024 }
0025 
0026 void TypesListTreeWidget::keyPressEvent(QKeyEvent *event)
0027 {
0028     if (event->key() == Qt::Key_Space && selectionMode() == ExtendedSelection) {
0029         // for some reason pressing space only (de)selects one of the items
0030         // in the tree widget, this reimplements the behavior so that it
0031         // toggles every selected item
0032 
0033         QTreeWidgetItemIterator it(this);
0034         bool first = true;
0035         Qt::CheckState state = Qt::Unchecked;
0036 
0037         while (*it) {
0038             QTreeWidgetItem *item = *it;
0039 
0040             if (item->isSelected() && item->data(0, Qt::CheckStateRole).isValid()) {
0041                 if (first) {
0042                     state = item->checkState(0);
0043 
0044                     if (item->checkState(0) != Qt::Checked) {
0045                         state = Qt::Checked;
0046                     } else {
0047                         state = Qt::Unchecked;
0048                     }
0049 
0050                     first = false;
0051                 }
0052 
0053                 item->setCheckState(0, state);
0054             }
0055 
0056             ++it;
0057         }
0058 
0059         event->accept();
0060     } else {
0061         QTreeWidget::keyPressEvent(event);
0062     }
0063 }
0064 
0065 #include "moc_typeslisttreewidget.cpp"