File indexing completed on 2024-05-19 05:42:25

0001 #include <ct_lvtqtd_removedelegate.h>
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <QApplication>
0021 #include <QDebug>
0022 #include <QModelIndex>
0023 #include <QMouseEvent>
0024 #include <QPainter>
0025 #include <QStyleOptionViewItem>
0026 
0027 namespace Codethink::lvtqtd {
0028 struct RemoveDelegate::Private { };
0029 
0030 RemoveDelegate::RemoveDelegate(QObject *parent):
0031     QStyledItemDelegate(parent), d(std::make_unique<RemoveDelegate::Private>())
0032 {
0033 }
0034 
0035 RemoveDelegate::~RemoveDelegate() noexcept = default;
0036 
0037 void RemoveDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
0038 {
0039     QStyledItemDelegate::paint(painter, option, index);
0040     if (!index.isValid()) {
0041         return;
0042     }
0043 
0044     /////////////////////////////////////////////////////////////////
0045     //     I have a Rect on the QComboBox like this:
0046     //     ########################
0047     //     # TEXT HERE            #
0048     //     ########################
0049     //
0050     //     and I want to put a button on the right side of the box,
0051     //     since I want the button to be a square, I use the height
0052     //     dimensions for it.
0053     //     This will generate a button like
0054     //
0055     //     ####
0056     //     #  #
0057     //     ####
0058     //
0059     //    now I need to tell the painter where to paint this.
0060     //    For this, I get the option.rect (that gives me the full rect)
0061     //    the new X of this rect will be the starting point of this button,
0062     //    that's x() + (width() - buttonWidth()) - but the buttonWidth is
0063     //    the same as the rect.height()
0064     //    So I end up with
0065     //
0066     //    #########################
0067     //    # TEXT HERE          #  #
0068     //    #########################
0069     ////////////////////////////////////////////////////////////////////
0070 
0071     QRect rect = option.rect.adjusted(1, 1, -1, -1);
0072     rect.setX(rect.x() + rect.width() - rect.height());
0073     rect.setWidth(rect.height());
0074 
0075     QStyleOptionButton opt;
0076     opt.state |= QStyle::State_Enabled;
0077     opt.rect = rect;
0078     opt.text = QStringLiteral("X");
0079     QApplication::style()->drawControl(QStyle::CE_PushButton, &opt, painter, nullptr);
0080 }
0081 
0082 bool RemoveDelegate::editorEvent(QEvent *event,
0083                                  QAbstractItemModel *model,
0084                                  const QStyleOptionViewItem& option,
0085                                  const QModelIndex& index)
0086 {
0087     if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick) {
0088         auto *mouseEvent = dynamic_cast<QMouseEvent *>(event);
0089         const auto pos = mouseEvent->localPos().toPoint();
0090 
0091         QRect rect = option.rect.adjusted(1, 1, -1, -1);
0092         rect.setX(rect.x() + rect.width() - rect.height());
0093         rect.setWidth(rect.height());
0094 
0095         if (rect.contains(pos)) {
0096             model->removeRow(index.row());
0097             return true; // event is handled.
0098         }
0099     }
0100 
0101     return QStyledItemDelegate::editorEvent(event, model, option, index);
0102 }
0103 } // namespace Codethink::lvtqtd