1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2023-05-15
 * Description : geolocation engine based on Marble.
 *               (c) 2007-2022 Marble Team
 *               https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
 *
 * SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * ============================================================ */

#include "PluginItemDelegate.h"

// Qt includes

#include <QEvent>
#include <QSize>
#include <QVariant>
#include <QAbstractItemView>
#include <QMouseEvent>
#include <QStandardItemModel>
#include <QApplication>
#include <QPainter>

// KDE includes

#include <klocalizedstring.h>

// Local includes

#include "RenderPluginModel.h"
#include "digikam_debug.h"

namespace Marble
{

const QSize iconSize(16, 16);

PluginItemDelegate::PluginItemDelegate(QAbstractItemView* view, QObject* parent)
    : QAbstractItemDelegate(parent)
{
    // Enable mouse tracking of itemview makes it possible to find when the mouse if moved
    // without pressed buttons.
    view->setMouseTracking(true);
}

PluginItemDelegate::~PluginItemDelegate()
{
}

void PluginItemDelegate::paint(QPainter* painter,
                               const QStyleOptionViewItem& option,
                               const QModelIndex& index) const
{
    Q_ASSERT(index.isValid());
    QRect rect = option.rect;
    QStyle* style = QApplication::style();

    painter->save();

    // Drawing the background
    QStyleOption background = option;
    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);

    painter->translate(rect.topLeft());

    // rect is now represented in item coordinates
    rect.moveTopLeft(QPoint(0, 0));
    // The point at the top left of the available drawing area.
    QPoint topLeft(0, 0);
    // The point at the top right of the available drawing area.
    QPoint topRight(rect.topRight());

    QRect nameRect = rect;

    // Painting the checkbox
    QStyleOptionButton checkBox = checkboxOption(option, index, topLeft.x(), Qt::AlignLeft);
    painter->save();
    style->drawControl(QStyle::CE_CheckBox, &checkBox, painter);
    painter->restore();

    nameRect.setLeft(checkBox.rect.right() + 1);

    // Painting the About Button
    QStyleOptionButton button = buttonOption(option, index, PluginItemDelegate::About,<--- Shadowed declaration
                                             topRight.x(), Qt::AlignRight);
    style->drawControl(QStyle::CE_PushButton, &button, painter);
    topRight -= QPoint(button.rect.width(), 0);

    // Painting the Configure Button
    if (index.data(RenderPluginModel::ConfigurationDialogAvailable).toBool())
    {
        QStyleOptionButton button = buttonOption(option, index, PluginItemDelegate::Configure,<--- Shadow variable
                                                 topRight.x(), Qt::AlignRight);
        style->drawControl(QStyle::CE_PushButton, &button, painter);
        topRight -= QPoint(button.rect.width(), 0);

        nameRect.setRight(button.rect.left() - 1);
    }

    // Painting the Icon
    const QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
    const QPixmap iconPixmap = icon.pixmap(16, 16);

    nameRect.moveBottom(nameRect.bottom() + 5);
    style->drawItemPixmap(painter,
                          nameRect,
                          Qt::AlignLeft,
                          iconPixmap);

    nameRect.setLeft(nameRect.left() + 16 + 5);
    nameRect.moveBottom(nameRect.bottom() - 5);

    // Painting the Name string
    QString name = index.data(Qt::DisplayRole).toString();

    style->drawItemText(painter,
                        nameRect,
                        Qt::AlignLeft | Qt::AlignVCenter,
                        option.palette,
                        true,
                        name);

    painter->restore();
}

QSize PluginItemDelegate::sizeHint(const QStyleOptionViewItem& option,
                                   const QModelIndex& index) const
{
    QSize size;

    QStyleOptionViewItem opt = option;
    opt.rect = QRect(0, 0, 0, 0);
    QVector<QSize> elementSize;
    elementSize.reserve(4);
    QStyleOptionButton checkBox = checkboxOption(opt, index);
    elementSize.append(checkBox.rect.size());
    QStyleOptionButton aboutButton = buttonOption(opt, index, PluginItemDelegate::About);
    elementSize.append(aboutButton.rect.size());
    QStyleOptionButton configButton = buttonOption(opt, index, PluginItemDelegate::Configure);
    elementSize.append(configButton.rect.size());
    elementSize.append(nameSize(index));

    for (const QSize& buttonSize : elementSize)
    {
        if (buttonSize.height() > size.height())
        {
            size.setHeight(buttonSize.height());
        }

        size.setWidth(size.width() + buttonSize.width());
    }

    return size;
}

void PluginItemDelegate::setAboutIcon(const QIcon& icon)
{
    m_aboutIcon = icon;
}

void PluginItemDelegate::setConfigIcon(const QIcon& icon)
{
    m_configIcon = icon;
}

bool PluginItemDelegate::editorEvent(QEvent* event,
                                     QAbstractItemModel* model,
                                     const QStyleOptionViewItem& option,
                                     const QModelIndex& index)
{
    Q_ASSERT(event);
    Q_ASSERT(model);

    if ((event->type() == QEvent::MouseButtonRelease)
        || (event->type() == QEvent::MouseButtonDblClick)
        || (event->type() == QEvent::MouseButtonPress)
        || (event->type() == QEvent::MouseMove))
    {
        QMouseEvent* me = static_cast<QMouseEvent*>(event);
        QPoint mousePosition = me->pos() - option.rect.topLeft();

        if ((event->type() == QEvent::MouseMove)
            && !(me->buttons() & Qt::LeftButton))
        {
            // If the mouse moves around and no left button is pressed, no pushbutton is pressed
            // and no other event will be successful.
            m_aboutPressedIndex = QModelIndex();
            m_configPressedIndex = QModelIndex();
            return true;
        }

        // Handle checkbox
        QRect checkRect = checkboxOption(option, index, 0, Qt::AlignLeft).rect;

        if (checkRect.contains(mousePosition)
            && ((event->type() == QEvent::MouseButtonDblClick)
                || (event->type() == QEvent::MouseButtonRelease)))
        {
            // make sure that the item is checkable
            Qt::ItemFlags flags = model->flags(index);

            if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
                || !(flags & Qt::ItemIsEnabled))
            {
                return false;
            }

            // make sure that we have a check state
            QVariant checkValue = index.data(Qt::CheckStateRole);

            if (!checkValue.isValid())
            {
                return false;
            }

            // eat the double click events inside the check rect
            if (event->type() == QEvent::MouseButtonDblClick)
            {
                return true;
            }

            Qt::CheckState state = (static_cast<Qt::CheckState>(checkValue.toInt()) == Qt::Checked
                                    ? Qt::Unchecked : Qt::Checked);
            return model->setData(index, state, Qt::CheckStateRole);
        }

        if ((event->type() == QEvent::MouseMove)
            && !(me->buttons() & Qt::LeftButton))
        {
            m_aboutPressedIndex = QModelIndex();
            m_configPressedIndex = QModelIndex();
            return true;
        }

        QPoint topRight = option.rect.topRight();

        // Handle aboutButton
        {
            QRect aboutRect = buttonOption(option,
                                           index,
                                           PluginItemDelegate::About,
                                           topRight.x(),
                                           Qt::AlignRight).rect;

            if (aboutRect.contains(mousePosition))
            {
                if (event->type() == QEvent::MouseButtonDblClick)
                {
                    return true;
                }

                if (event->type() == QEvent::MouseButtonPress)
                {
                    m_aboutPressedIndex = index;
                    m_configPressedIndex = QModelIndex();
                    return true;
                }

                if (event->type() == QEvent::MouseButtonRelease)
                {
                    m_aboutPressedIndex = QModelIndex();
                    m_configPressedIndex = QModelIndex();
                    Q_EMIT aboutPluginClicked(index);
                    return true;
                }

                if (event->type() == QEvent::MouseMove)
                {
                    if (me->buttons() & Qt::LeftButton)
                    {
                        m_aboutPressedIndex = index;
                        m_configPressedIndex = QModelIndex();
                        return true;
                    }

                    else
                    {
                        m_aboutPressedIndex = QModelIndex();
                        m_configPressedIndex = QModelIndex();
                        return true;
                    }
                }
            }

            else
            {
                // If the mouse is on the item and the mouse isn't above the button.
                // no about button is pressed.
                m_aboutPressedIndex = QModelIndex();
            }

            topRight -= QPoint(aboutRect.width(), 0);
        }

        // Handle configButton
        // make sure we have config button
        if (index.data(RenderPluginModel::ConfigurationDialogAvailable).toBool())
        {
            QRect configRect = buttonOption(option,
                                            index,
                                            PluginItemDelegate::Configure,
                                            topRight.x(),
                                            Qt::AlignRight).rect;

            if (configRect.contains(mousePosition))
            {
                if (event->type() == QEvent::MouseButtonDblClick)
                {
                    return true;
                }

                if (event->type() == QEvent::MouseButtonPress)
                {
                    m_aboutPressedIndex = QModelIndex();
                    m_configPressedIndex = index;
                    return true;
                }

                if (event->type() == QEvent::MouseButtonRelease)
                {
                    m_aboutPressedIndex = QModelIndex();
                    m_configPressedIndex = QModelIndex();
                    Q_EMIT configPluginClicked(index);
                    return true;
                }

                if (event->type() == QEvent::MouseMove)
                {
                    if (me->buttons() & Qt::LeftButton)
                    {
                        m_aboutPressedIndex = QModelIndex();
                        m_configPressedIndex = index;
                        return true;
                    }

                    else
                    {
                        m_aboutPressedIndex = QModelIndex();
                        m_configPressedIndex = QModelIndex();
                        return true;
                    }
                }
            }

            else
            {
                // If the mouse is on the item and the mouse isn't above the button.
                // no config button is pressed.
                m_configPressedIndex = QModelIndex();
            }

            topRight -= QPoint(configRect.width(), 0);
        }

        else
        {
            // If we don't have an config dialog shown and the mouse is over this item,
            // no config button is pressed.
            m_configPressedIndex = QModelIndex();
        }
    }

    return false;
}

QStyleOptionButton PluginItemDelegate::checkboxOption(const QStyleOptionViewItem& option,
                                                      const QModelIndex& index,
                                                      int position,
                                                      Qt::AlignmentFlag alignment)
{
    QStyleOptionButton styleOptionButton;

    if (index.data(Qt::CheckStateRole).toBool())
    {
        styleOptionButton.state = option.state | QStyle::State_On;
    }

    else
    {
        styleOptionButton.state = option.state | QStyle::State_Off;
    }

    QSize size = QApplication::style()->sizeFromContents(QStyle::CT_CheckBox, &option, QSize());

    if (size.isEmpty())
    {
        // A checkbox has definitely a size != 0
        styleOptionButton.rect.setSize(QSize(22, 22));
    }

    else
    {
        styleOptionButton.rect.setSize(QSize(size.width(), size.height()));
    }

    styleOptionButton.rect = alignRect(styleOptionButton.rect, option.rect, position, alignment);
    return styleOptionButton;
}

QStyleOptionButton PluginItemDelegate::buttonOption(const QStyleOptionViewItem& option,
                                                    const QModelIndex& index,
                                                    PluginItemDelegate::ButtonType type,
                                                    int position,
                                                    Qt::AlignmentFlag alignment) const
{
    QStyleOptionButton buttonOption;
    buttonOption.state = option.state;
    buttonOption.state &= ~QStyle::State_HasFocus;

    buttonOption.rect.setTopLeft(QPoint(0, 0));
    buttonOption.palette = option.palette;
    buttonOption.features = QStyleOptionButton::None;

    QSize contentSize;

    if (type == PluginItemDelegate::About)
    {
        if (m_aboutIcon.isNull())
        {
            buttonOption.text = i18n("About");
            contentSize = buttonOption.fontMetrics.size(0, buttonOption.text) + QSize(4, 4);
        }

        else
        {
            buttonOption.icon = m_aboutIcon;
            buttonOption.iconSize = iconSize;
            contentSize = iconSize;
        }

        if (m_aboutPressedIndex == index)
        {
            buttonOption.state |= QStyle::State_Sunken;
        }
    }

    else if (type == PluginItemDelegate::Configure)
    {
        if (m_configIcon.isNull())
        {
            buttonOption.text = i18n("Configure");
            contentSize = buttonOption.fontMetrics.size(0, buttonOption.text) + QSize(4, 4);
        }

        else
        {
            buttonOption.icon = m_configIcon;
            buttonOption.iconSize = iconSize;
            contentSize = iconSize;
        }

        if (m_configPressedIndex == index)
        {
            buttonOption.state |= QStyle::State_Sunken;
        }
    }

    QSize buttonSize = QApplication::style()->sizeFromContents(QStyle::CT_PushButton,
                                                               &buttonOption,
                                                               contentSize);
    buttonOption.rect.setSize(buttonSize);
    buttonOption.rect = alignRect(buttonOption.rect, option.rect, position, alignment);
    return buttonOption;
}

QSize PluginItemDelegate::nameSize(const QModelIndex& index)
{
    QString name = index.data(Qt::DisplayRole).toString();
    // FIXME: QApplication::fontMetrics() doesn't work for non-application fonts
    QSize nameSize(QFontMetrics(qApp->font()).size(0, name));
    return nameSize;
}

QRect PluginItemDelegate::alignRect(const QRect& object,
                                    const QRect& frame,
                                    int position,
                                    Qt::AlignmentFlag alignment)
{
    QRect rect = object;

    rect.setTopLeft(QPoint(0, 0));

    // Moves the object to the middle of the item.
    if (rect.height() < frame.height())
    {
        rect.moveTop((frame.height() - rect.height()) / 2);
    }

    if (alignment & Qt::AlignLeft)
    {
        rect.moveLeft(position);
    }

    else if (alignment & Qt::AlignRight)
    {
        rect.moveRight(position);
    }

    return rect;
}

} // namespace Marble

#include "moc_PluginItemDelegate.cpp"