ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvOverlayDialog.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "ecvOverlayDialog.h"
9 
10 // CV_CORE_LIB
11 #include <CVLog.h>
12 
13 // CV_DB_LIB
14 #include <ecvDisplayTools.h>
15 
16 // Qt
17 #include <QApplication>
18 #include <QEvent>
19 #include <QKeyEvent>
20 #include <QMessageBox>
21 
22 // system
23 #include <cassert>
24 
26  QWidget* parent /*=0*/,
27  Qt::WindowFlags flags /*=Qt::FramelessWindowHint | Qt::Tool*/)
28  : QDialog(parent, flags), m_associatedWin(nullptr), m_processing(false) {}
29 
31 
32 bool ccOverlayDialog::linkWith(QWidget* win) {
33  if (m_processing) {
35  "[ccOverlayDialog] Can't change associated window while "
36  "running/displayed!");
37  return false;
38  }
39 
40  // same dialog? nothing to do
41  if (m_associatedWin == win) {
42  return true;
43  }
44 
45  if (m_associatedWin) {
46  // we automatically detach the former dialog
47  {
48  QWidgetList topWidgets = QApplication::topLevelWidgets();
49  foreach (QWidget* widget, topWidgets) {
50  widget->removeEventFilter(this);
51  }
52  m_associatedWin->removeEventFilter(this);
53  disconnect(m_associatedWin, &QObject::destroyed, this,
55  m_associatedWin->disconnect(this);
56  m_associatedWin = nullptr;
57  }
58  }
59 
60  m_associatedWin = win;
61  if (m_associatedWin) {
62  QWidgetList topWidgets = QApplication::topLevelWidgets();
63  foreach (QWidget* widget, topWidgets) {
64  widget->installEventFilter(this);
65  }
66  m_associatedWin->installEventFilter(this);
67  connect(m_associatedWin, &QObject::destroyed, this,
69  }
70 
71  return true;
72 }
73 
74 void ccOverlayDialog::onLinkedWindowDeletion(QObject* object /*=0*/) {
75  if (m_processing) stop(false);
76 
77  linkWith(nullptr);
78 }
79 
81  if (m_processing) return false;
82 
83  m_processing = true;
84 
85  // auto-show
86  show();
87 
88  return true;
89 }
90 
91 void ccOverlayDialog::stop(bool accepted) {
92  m_processing = false;
93 
94  // auto-hide
95  hide();
96 
97  linkWith(nullptr);
98 
99  emit processFinished(accepted);
100 }
101 
103  if (QMessageBox::question(
104  this, tr("Quit"), tr("Are you sure you want to quit dialog?"),
105  QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Cancel) {
106  return;
107  }
108 
109  QDialog::reject();
110 
111  stop(false);
112 }
113 
115  m_overriddenKeys.push_back(key);
116 }
117 
118 bool ccOverlayDialog::eventFilter(QObject* obj, QEvent* e) {
119  if (e->type() == QEvent::KeyPress) {
120  QKeyEvent* keyEvent = static_cast<QKeyEvent*>(e);
121 
122  if (m_overriddenKeys.contains(keyEvent->key())) {
123  emit shortcutTriggered(keyEvent->key());
124  return true;
125  } else {
126  return QDialog::eventFilter(obj, e);
127  }
128  } else {
129  if (e->type() == QEvent::Show) {
130  emit shown();
131  }
132 
133  // standard event processing
134  return QDialog::eventFilter(obj, e);
135  }
136 }
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
ccOverlayDialog(QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::FramelessWindowHint|Qt::Tool)
Default constructor.
void reject() override
void shown()
Signal emitted when a 'show' event is detected.
void shortcutTriggered(int key)
Signal emitted when an overridden key shortcut is pressed.
virtual void onLinkedWindowDeletion(QObject *object=nullptr)
Slot called when the linked window is deleted (calls 'onClose')
virtual void stop(bool accepted)
Stops process/dialog.
QList< int > m_overriddenKeys
Overridden keys.
virtual bool start()
Starts process.
bool m_processing
Running/processing state.
void processFinished(bool accepted)
Signal emitted when process is finished.
QWidget * m_associatedWin
Associated (MDI) window.
~ccOverlayDialog() override
Destructor.
virtual bool linkWith(QWidget *win)
Links the overlay dialog with a MDI window.
bool eventFilter(QObject *obj, QEvent *e) override
void addOverridenShortcut(Qt::Key key)