File indexing completed on 2024-04-28 16:26:30

0001 /***************************************************************************
0002 date                 : Dec 23 2007
0003 version              : 0.22
0004 copyright            : (C) 2004 by Mathias Soeken
0005 email                : msoeken@tzi.de
0006 ***************************************************************************/
0007 
0008 /***************************************************************************
0009  *                                                                         *
0010  *   This program is free software; you can redistribute it and/or modify  *
0011  *   it under the terms of the GNU General Public License as published by  *
0012  *   the Free Software Foundation; either version 2 of the License, or     *
0013  *   (at your option) any later version.                                   *
0014  *                                                                         *
0015  ***************************************************************************/
0016 
0017 #include "categorycombobox.h"
0018 
0019 #include <QAbstractItemView>
0020 #include <QItemDelegate>
0021 #include <QPainter>
0022 #include <QStandardItem>
0023 #include <QStandardItemModel>
0024 
0025 namespace KileWidget {
0026 
0027 class CategoryComboBoxDelegate : public QItemDelegate {
0028 public:
0029     void paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
0030         bool category = index.model()->data(index, KileWidget::CategoryComboBox::Category).toBool();
0031 
0032         if (category) {
0033             painter->setPen(Qt::gray);
0034             painter->drawLine(option.rect.x(), option.rect.y() + (option.rect.height() / 2), option.rect.x() + 4, option.rect.y() + (option.rect.height() / 2));
0035 
0036             QFont newFont = painter->font();
0037             newFont.setPointSize(8);
0038             painter->setFont(newFont);
0039 
0040             QRect boundingRect;
0041             painter->drawText(QRect(option.rect.x() + 5, option.rect.y(), option.rect.width() - 5, option.rect.height()), Qt::AlignLeft | Qt::AlignVCenter, index.model()->data(index, Qt::DisplayRole).toString(), &boundingRect);
0042 
0043             painter->drawLine(boundingRect.right() + 1, option.rect.y() + (option.rect.height() / 2), option.rect.right(), option.rect.y() + (option.rect.height() / 2));
0044         }
0045         else {
0046             QItemDelegate::paint(painter, option, index);
0047         }
0048     }
0049 };
0050 
0051 CategoryComboBox::CategoryComboBox(QWidget *parent) : KComboBox(parent)
0052 {
0053     setItemDelegate(new KileWidget::CategoryComboBoxDelegate());
0054     //view()->setAlternatingRowColors( true );
0055 }
0056 
0057 CategoryComboBox::CategoryComboBox(bool rw, QWidget *parent) : KComboBox(rw, parent)
0058 {
0059     setItemDelegate(new KileWidget::CategoryComboBoxDelegate());
0060     //view()->setAlternatingRowColors( true );
0061 }
0062 
0063 CategoryComboBox::~CategoryComboBox()
0064 {
0065 }
0066 
0067 void CategoryComboBox::addCategoryItem(const QString &text)
0068 {
0069     addItem(text);
0070 
0071     // row of the item
0072     int row = count() - 1;
0073 
0074     QStandardItemModel *pModel = qobject_cast<QStandardItemModel*>(model());
0075     if (pModel) {
0076         QStandardItem *item = pModel->item(row, 0);
0077         if (item) {
0078             item->setData(true, KileWidget::CategoryComboBox::Category);
0079 
0080             // make the item unselectable
0081             item->setFlags(Qt::NoItemFlags);
0082         }
0083     }
0084 
0085     if (currentIndex() == row) {
0086         setCurrentIndex(-1);
0087     }
0088 }
0089 
0090 } // namespace KileWidget