File: dlgmessage.cpp

package info (click to toggle)
guymager 0.8.13-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,764 kB
  • sloc: cpp: 17,479; makefile: 30; sh: 22
file content (245 lines) | stat: -rw-r--r-- 9,179 bytes parent folder | download | duplicates (2)
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
// ****************************************************************************
//  Project:        GUYMAGER
// ****************************************************************************
//  Programmer:     Guy Voncken
//                  Police Grand-Ducale
//                  Service de Police Judiciaire
//                  Section Nouvelles Technologies
// ****************************************************************************
//  Module:         Different message boxes we need all the time
// ****************************************************************************

// Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
// 2018, 2019, 2020
// Guy Voncken
//
// This file is part of Guymager.
//
// Guymager is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Guymager is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Guymager. If not, see <http://www.gnu.org/licenses/>.

#if (QT_VERSION >= 0x050000)
   #include <QtWidgets> //lint !e537 Repeated include
#else
   #include <QtGui>     //lint !e537 Repeated include
#endif

#include "dlgmessage.h"
#include "config.h"
#include "qtutil.h"

// -----------------------------
//           Constants
// -----------------------------

const int DLGMESSAGE_MAX_SCREEN_PERCENTAGE = 80;  // When opening the dialog, do not use more than 80% of the screen width / height

// -----------------------------
//           Classes
// -----------------------------

class t_DlgMessageLocal
{
   public:
      QVBoxLayout *pLayout;
      QTextEdit   *pTextBox;
      QPushButton *pButtonClose;
};

t_DlgMessage::t_DlgMessage ()
{
   CHK_EXIT (ERROR_DLGMESSAGE_CONSTRUCTOR_NOT_SUPPORTED)
} //lint !e1401 pOwn not initialised

t_DlgMessage::t_DlgMessage (const QString &Title, QString Message, bool Monospaced, QWidget *pParent, Qt::WindowFlags Flags)
   :QDialog (pParent, Flags)
{
   static bool  Initialised = false;
   QFont      *pFont;

   if (!Initialised)
   {
      Initialised = true;
      CHK_EXIT (TOOL_ERROR_REGISTER_CODE (ERROR_DLGMESSAGE_CONSTRUCTOR_NOT_SUPPORTED))
   }

   QTUTIL_SET_FONT (this, FONTOBJECT_MESSAGE_DIALOGS)

   pOwn = new t_DlgMessageLocal;
   pOwn->pLayout  = new QVBoxLayout ();
   pOwn->pTextBox = new QTextEdit   (this);

   pFont = CfgGetpFont (FONTOBJECT_DIALOG_DATA);  // Set the font, if available
   if (pFont)
      (pOwn->pTextBox)->setFont(*pFont);

   if (Monospaced && !pFont)
   {
      Message.prepend ("<pre>" ); // If no font was specified, ensure that
      Message.append  ("</pre>"); // a standard monospaced font is used
   }
   else
   {
      Message.replace ("\r\n", "<br>");       // Translate line breaks into HTML tags
      Message.replace ("\n"  , "<br>");
   }

   pOwn->pTextBox->setWordWrapMode (QTextOption::NoWrap);
   pOwn->pTextBox->setAcceptRichText (true);
   pOwn->pTextBox->setHtml (Message);

   pOwn->pButtonClose = new QPushButton (tr("Close"), this);

   pOwn->pLayout->addWidget (pOwn->pTextBox    );
   pOwn->pLayout->addWidget (pOwn->pButtonClose);

   pOwn->pTextBox->setReadOnly (true);
   pOwn->pTextBox->zoomIn ();

   setLayout (pOwn->pLayout);
   setWindowTitle (Title);

   CHK_QT_EXIT (connect (pOwn->pButtonClose, SIGNAL (released()), this, SLOT(accept())))
}

void t_DlgMessage::AdjustGeometry (void)
{
   int X, Y;
   int Dx, Dy;

   // Adjust size
   // -----------
   // Trying to adjust the size of the dialog to size of the text inside. Not easy, as Qt doesn't provide the right functions for
   // doing so. Important: AdjustSize must be called after the dialog has been showed in order have QTextBox calculated all scroll
   // bar values.
   Dx  = pOwn->pTextBox->horizontalScrollBar()->maximum();     // See source code of QAbstractScrollArea: The scroll bar range is the full
   Dy  = pOwn->pTextBox->verticalScrollBar  ()->maximum();     // scroll area width minus the window area displayed. So, we, take the
   Dx += pOwn->pTextBox->viewport()->width ();                 // scroll bar range and add the viewport size.
   Dy += pOwn->pTextBox->viewport()->height();
   Dx += pOwn->pTextBox->verticalScrollBar  ()->height();      // Scrollbars need some place as well...
   Dy += pOwn->pTextBox->horizontalScrollBar()->height();
   Dx += frameGeometry().width () - pOwn->pTextBox->width ();  // Add the space surrounding the Textbox required by the dialog
   Dy += frameGeometry().height() - pOwn->pTextBox->height();

   Dx = GETMIN (Dx, (DLGMESSAGE_MAX_SCREEN_PERCENTAGE * QApplication::desktop()->width ()) / 100);  // Limit to a certain amount
   Dy = GETMIN (Dy, (DLGMESSAGE_MAX_SCREEN_PERCENTAGE * QApplication::desktop()->height()) / 100);  // of the available screen space


   // Adjust position
   // ---------------
   // Try to center relative to the parent if available, relative to srceen otherwise

   QWidget *pParent = parentWidget();
   if (pParent)
   {
      QPoint PosRel (pParent->x(), pParent->y());
      QPoint PosAbs = pParent->mapToGlobal(PosRel);

      X = PosAbs.x() + pParent->width ()/2 - Dx/2;
      Y = PosAbs.y() + pParent->height()/2 - Dy/2;
   }
   else
   {
      X = (QApplication::desktop()->width  () - Dx) / 2;
      Y = (QApplication::desktop()->height () - Dy) / 2;
   }

   setGeometry (X, Y, Dx, Dy);
}

t_DlgMessage::~t_DlgMessage ()
{
   delete pOwn;
}

APIRET t_DlgMessage::Show (const QString &Title, const QString &Message, bool Monospaced, QWidget *pParent, Qt::WindowFlags Flags)
{
   t_DlgMessage *pDlg;

   pDlg = new t_DlgMessage (Title, Message, Monospaced, pParent, Flags);
   pDlg->setModal      (true);
   pDlg->show          ();
   pDlg->AdjustGeometry();
   pDlg->exec          ();
   delete pDlg;

   return NO_ERROR;
}

// ----------------------------------------------------------------
//  t_MessageBox can be used as a replacement for many QMessageBox
//  dialogs. The difference is, that t_MessageBox sets the font
//  The main parts of the code below has been copied from Qt
//  source code in src/gui/dialogs/qmessagebox.cpp
// ----------------------------------------------------------------

t_MessageBox::t_MessageBox (QWidget *pParent) :
   QMessageBox (pParent)
{
   QTUTIL_SET_FONT (this, FONTOBJECT_MESSAGE_DIALOGS)
}

t_MessageBox::t_MessageBox (Icon icon, const QString & title, const QString & text, StandardButtons buttons, QWidget *parent, Qt::WindowFlags f) :
   QMessageBox (icon, title, text, buttons, parent, f)
{
   QTUTIL_SET_FONT (this, FONTOBJECT_MESSAGE_DIALOGS)
}

t_MessageBox::~t_MessageBox ()
{
}

QMessageBox::StandardButton t_MessageBox::showNewMessageBox(QWidget *pParent, QMessageBox::Icon Icon, const QString &Title, const QString &Text, QMessageBox::StandardButtons Buttons, QMessageBox::StandardButton DefaultButton)
{
   t_MessageBox       MsgBox (Icon, Title, Text, QMessageBox::NoButton, pParent);
   QDialogButtonBox *pButtonBox = MsgBox.findChild<QDialogButtonBox*>();

   Q_ASSERT (pButtonBox != nullptr);
   uint ButtonMask = QMessageBox::FirstButton;
   while (ButtonMask <= QMessageBox::LastButton)
   {
      uint CurrentButton = Buttons & ButtonMask;
      ButtonMask <<= 1;
      if (!CurrentButton)
         continue;
      QPushButton *pPushbutton = MsgBox.addButton ((QMessageBox::StandardButton)CurrentButton);
      if (MsgBox.defaultButton())  // Do we already have set the  default button?
         continue;
      if ((DefaultButton == QMessageBox::NoButton && pButtonBox->buttonRole(pPushbutton) == QDialogButtonBox::AcceptRole) ||
          (DefaultButton != QMessageBox::NoButton && CurrentButton == uint(DefaultButton)))
         MsgBox.setDefaultButton (pPushbutton);
   }
   if (MsgBox.exec() == -1)
      return QMessageBox::Cancel;
   return MsgBox.standardButton (MsgBox.clickedButton());
}

QMessageBox::StandardButton t_MessageBox::question(QWidget *parent, const QString &title, const QString& text, StandardButtons buttons, StandardButton defaultButton)
{
    return showNewMessageBox (parent, Question, title, text, buttons, defaultButton);
}

QMessageBox::StandardButton t_MessageBox::information(QWidget *parent, const QString &title, const QString& text, StandardButtons buttons, StandardButton defaultButton)
{
   return showNewMessageBox(parent, Information, title, text, buttons,defaultButton);
}

QMessageBox::StandardButton t_MessageBox::warning(QWidget *parent, const QString &title, const QString& text, StandardButtons buttons, StandardButton defaultButton)
{
    return showNewMessageBox(parent, Warning, title, text, buttons, defaultButton);
}

QMessageBox::StandardButton t_MessageBox::critical(QWidget *parent, const QString &title, const QString& text, StandardButtons buttons, StandardButton defaultButton)
{
    return showNewMessageBox(parent, Critical, title, text, buttons, defaultButton);
}