Seamly2D
Code documentation
vwidgetpopup.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file vwidgetpopup.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 16 2, 2015
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2013-2015 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #include "vwidgetpopup.h"
53 
54 #include <QScreen>
55 #include <QGuiApplication>
56 #include <QFont>
57 #include <QLabel>
58 #include <QLayout>
59 #include <QMessageLogger>
60 #include <QPoint>
61 #include <QRect>
62 #include <QTimer>
63 #include <QVBoxLayout>
64 #include <QWidget>
65 #include <Qt>
66 #include <QApplication>
67 #include <QScreen>
68 
69 #include "../vmisc/def.h"
70 
71 //---------------------------------------------------------------------------------------------------------------------
73  :QFrame(parent, Qt::Popup), mWidget(nullptr), mOwn(true), mOldParent(nullptr), lifeTime(-1)
74 {
75  setAttribute(Qt::WA_WindowPropagation);
76 
77  if (parentWidget() == nullptr)
78  {
79  setAttribute(Qt::WA_DeleteOnClose);
80  }
81 
82  setLayout(new QVBoxLayout());
83  layout()->setContentsMargins(0, 0, 0, 0);
84 }
85 
86 //---------------------------------------------------------------------------------------------------------------------
87 void VWidgetPopup::SetWidget(QWidget *widget, bool own)
88 {
89  if (mWidget)
90  {
91  layout()->removeWidget(mWidget);
92 
93  if (mOwn)
94  {
95  mWidget->setParent(nullptr);
96  delete mWidget;
97  }
98  else
99  {
100  mWidget->setParent(mOldParent);
101  }
102  }
103 
104  mWidget = widget;
105  mOwn = own;
106  mOldParent = nullptr;
107 
108  if (mWidget)
109  {
110  mOldParent = mWidget->parentWidget();
111  mWidget->setParent(this);
112  layout()->addWidget(mWidget);
113  }
114 }
115 
116 //---------------------------------------------------------------------------------------------------------------------
117 void VWidgetPopup::PopupMessage(QWidget *w, const QString &msg)
118 {
119  SCASSERT(w != nullptr)
120 
121  VWidgetPopup *popup = new VWidgetPopup(w);
122  QLabel *label = new QLabel(msg);
123  QFont f = label->font();
124  f.setBold(true);
125  f.setPixelSize(16);
126  label->setFont(f);
127  popup->SetWidget(label);
128  popup->SetLifeTime(2000);
129  popup->Show(w->frameGeometry().center());
130 }
131 
132 //---------------------------------------------------------------------------------------------------------------------
133 void VWidgetPopup::Show(QPoint coord)
134 {
135  // important to do this before following adjustments!
136  QFrame::show();
137 
138  const QRect screen(QGuiApplication::primaryScreen()->availableGeometry());
139  coord.setX(coord.x() - width()/2);
140 
141  if (coord.x() < screen.x())
142  {
143  coord.setX(screen.x());
144  }
145 
146  if (coord.y() < screen.y())
147  {
148  coord.setY(screen.y());
149  }
150 
151  if (coord.x() > (screen.right()-width()))
152  {
153  coord.setX(screen.right()-width());
154  }
155 
156  if (coord.y() > (screen.bottom()-height()))
157  {
158  coord.setY(screen.bottom()-height());
159  }
160  move(coord);
161 
162  if (lifeTime > 0)
163  {
164  QTimer::singleShot(lifeTime, this, SLOT(close()));
165  }
166 }
Class showing a widget as popup window.
Definition: vwidgetpopup.h:72
static void PopupMessage(QWidget *w, const QString &msg)
QWidget * mOldParent
Definition: vwidgetpopup.h:107
void SetWidget(QWidget *widget, bool own=true)
void Show(QPoint coord)
VWidgetPopup(QWidget *parent=nullptr)
void SetLifeTime(int value)
Definition: vwidgetpopup.h:130
QWidget * mWidget
Definition: vwidgetpopup.h:105
#define SCASSERT(cond)
Definition: def.h:317