swift
dropsite.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "gui/dropsite.h"
5 
6 #include <QDragEnterEvent>
7 #include <QDragLeaveEvent>
8 #include <QDragMoveEvent>
9 #include <QDropEvent>
10 #include <QFrame>
11 #include <QPalette>
12 #include <QStyle>
13 #include <Qt>
14 #include <QtGlobal>
15 
16 #include "gui/guiapplication.h"
17 #include "gui/stylesheetutility.h"
18 
19 using namespace swift::misc;
20 
21 namespace swift::gui
22 {
23  CDropSite::CDropSite(QWidget *parent) : QLabel(parent)
24  {
25  this->setFrameStyle(static_cast<int>(QFrame::Sunken) | QFrame::StyledPanel);
26  this->setAlignment(Qt::AlignCenter);
27  this->setAcceptDrops(true);
28  this->setTextFormat(Qt::RichText);
29  this->setInfoText("drop data here");
30  this->onStyleSheetsChanged();
31  connect(sGui, &CGuiApplication::styleSheetsChanged, this, &CDropSite::onStyleSheetsChanged,
32  Qt::QueuedConnection);
33  }
34 
35  void CDropSite::setInfoText(const QString &dropSiteText)
36  {
37  m_infoText = dropSiteText;
38  this->resetText();
39  }
40 
41  void CDropSite::allowDrop(bool allowed)
42  {
43  CDropBase::allowDrop(allowed);
44  this->setEnabled(allowed);
45  this->setVisible(allowed);
46  }
47 
48  void CDropSite::resetText()
49  {
50  const QString html = "<img src=':/own/icons/own/drophere16.png'>&nbsp;&nbsp;" + m_infoText.toHtmlEscaped();
51  setText(html);
52  }
53 
54  void CDropSite::dragEnterEvent(QDragEnterEvent *event)
55  {
56  if (!event || !acceptDrop(event->mimeData())) { return; }
57  setBackgroundRole(QPalette::Highlight);
58  event->acceptProposedAction();
59  }
60 
61  void CDropSite::dragMoveEvent(QDragMoveEvent *event)
62  {
63  if (!event || !acceptDrop(event->mimeData())) { return; }
64  setBackgroundRole(QPalette::Highlight);
65  event->acceptProposedAction();
66  }
67 
68  void CDropSite::dragLeaveEvent(QDragLeaveEvent *event)
69  {
70  if (!event || !m_allowDrop) { return; }
71  resetText();
72  event->accept();
73  }
74 
75  void CDropSite::dropEvent(QDropEvent *event)
76  {
77  if (!event || !acceptDrop(event->mimeData())) { return; }
78  CVariant valueVariant(toCVariant(event->mimeData()));
79  if (valueVariant.isValid()) { emit droppedValueObject(valueVariant); }
80  this->resetText();
81  }
82 
83  void CDropSite::onStyleSheetsChanged()
84  {
85  // style sheet changes go here
86  }
87 } // namespace swift::gui
bool acceptDrop(const QMimeData *mime) const
Accept drop?
Definition: dropbase.cpp:24
virtual void allowDrop(bool allowed)
Drop allowed.
Definition: dropbase.h:37
swift::misc::CVariant toCVariant(const QMimeData *mime) const
Mime data to CVariant (normally encapsulating a value object)
Definition: dropbase.cpp:43
void droppedValueObject(const swift::misc::CVariant &droppedObject)
Dropped value object.
virtual void dragEnterEvent(QDragEnterEvent *event)
Definition: dropsite.cpp:54
void setInfoText(const QString &dropSiteText)
Set text for drop site.
Definition: dropsite.cpp:35
virtual void dropEvent(QDropEvent *event)
Definition: dropsite.cpp:75
void allowDrop(bool allowed)
Drop allowed.
Definition: dropsite.cpp:41
virtual void dragLeaveEvent(QDragLeaveEvent *event)
Definition: dropsite.cpp:68
virtual void dragMoveEvent(QDragMoveEvent *event)
Definition: dropsite.cpp:61
void styleSheetsChanged()
Style sheet changed.
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
bool isValid() const
True if this variant is valid.
Definition: variant.h:252
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
GUI related classes.
Free functions in swift::misc.