aboutsummaryrefslogtreecommitdiff
path: root/libs/uibase/src/questionboxmemory.cpp
blob: 586e52eb1458efa19d44bf6c35da2b077dab7fd4 (plain)
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
/*
Mod Organizer shared UI functionality

Copyright (C) 2012 Sebastian Herbord. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <uibase/questionboxmemory.h>
#include <uibase/log.h>
#include "ui_questionboxmemory.h"

#include <QApplication>
#include <QIcon>
#include <QMutex>
#include <QMutexLocker>
#include <QPushButton>
#include <QSettings>
#include <QStyle>

namespace MOBase
{

static QMutex g_mutex;
static QuestionBoxMemory::GetButton g_get;
static QuestionBoxMemory::SetWindowButton g_setWindow;
static QuestionBoxMemory::SetFileButton g_setFile;

QuestionBoxMemory::QuestionBoxMemory(QWidget* parent, const QString& title,
                                     const QString& text, QString const* filename,
                                     const QDialogButtonBox::StandardButtons buttons,
                                     QDialogButtonBox::StandardButton defaultButton)
    : QDialog(parent), ui(new Ui::QuestionBoxMemory), m_Button(QDialogButtonBox::Cancel)
{
  ui->setupUi(this);

  setWindowFlag(Qt::WindowType::WindowContextHelpButtonHint, false);
  setWindowTitle(title);

  QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxQuestion);
  ui->iconLabel->setPixmap(icon.pixmap(128));
  ui->messageLabel->setText(text);

  if (filename == nullptr) {
    // delete the 2nd check box
    QCheckBox* box = ui->rememberForCheckBox;
    box->parentWidget()->layout()->removeWidget(box);
    delete box;
  } else {
    ui->rememberForCheckBox->setText(ui->rememberForCheckBox->text().arg(*filename));
  }

  ui->buttonBox->setStandardButtons(buttons);

  if (defaultButton != QDialogButtonBox::NoButton) {
    ui->buttonBox->button(defaultButton)->setDefault(true);
  }

  connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), this,
          SLOT(buttonClicked(QAbstractButton*)));
}

QuestionBoxMemory::~QuestionBoxMemory() = default;

void QuestionBoxMemory::setCallbacks(GetButton get, SetWindowButton setWindow,
                                     SetFileButton setFile)
{
  QMutexLocker locker(&g_mutex);

  g_get       = get;
  g_setWindow = setWindow;
  g_setFile   = setFile;
}

void QuestionBoxMemory::buttonClicked(QAbstractButton* button)
{
  m_Button = ui->buttonBox->standardButton(button);
}

QDialogButtonBox::StandardButton
QuestionBoxMemory::query(QWidget* parent, const QString& windowName,
                         const QString& title, const QString& text,
                         QDialogButtonBox::StandardButtons buttons,
                         QDialogButtonBox::StandardButton defaultButton)
{
  return queryImpl(parent, windowName, nullptr, title, text, buttons, defaultButton);
}

QDialogButtonBox::StandardButton
QuestionBoxMemory::query(QWidget* parent, const QString& windowName,
                         const QString& fileName, const QString& title,
                         const QString& text, QDialogButtonBox::StandardButtons buttons,
                         QDialogButtonBox::StandardButton defaultButton)
{
  return queryImpl(parent, windowName, &fileName, title, text, buttons, defaultButton);
}

QDialogButtonBox::StandardButton
QuestionBoxMemory::queryImpl(QWidget* parent, const QString& windowName,
                             const QString* fileName, const QString& title,
                             const QString& text,
                             QDialogButtonBox::StandardButtons buttons,
                             QDialogButtonBox::StandardButton defaultButton)
{
  QMutexLocker locker(&g_mutex);

  const auto button = getMemory(windowName, (fileName ? *fileName : ""));
  if (button != NoButton) {
    log::debug("{}: not asking because user always wants response {}",
               windowName + (fileName ? QString("/") + *fileName : ""),
               buttonToString(button));

    return button;
  }

  QuestionBoxMemory dialog(parent, title, text, fileName, buttons, defaultButton);
  dialog.exec();

  if (dialog.m_Button != QDialogButtonBox::Cancel) {
    if (dialog.ui->rememberCheckBox->isChecked()) {
      setWindowMemory(windowName, dialog.m_Button);
    }

    if (fileName != nullptr && dialog.ui->rememberForCheckBox->isChecked()) {
      setFileMemory(windowName, *fileName, dialog.m_Button);
    }
  }

  return dialog.m_Button;
}

void QuestionBoxMemory::setWindowMemory(const QString& windowName, Button b)
{
  log::debug("remembering choice {} for window {}", buttonToString(b), windowName);

  g_setWindow(windowName, b);
}

void QuestionBoxMemory::setFileMemory(const QString& windowName,
                                      const QString& filename, Button b)
{
  log::debug("remembering choice {} for file {}", buttonToString(b),
             windowName + "/" + filename);

  g_setFile(windowName, filename, b);
}

QuestionBoxMemory::Button QuestionBoxMemory::getMemory(const QString& windowName,
                                                       const QString& filename)
{
  return g_get(windowName, filename);
}

QString QuestionBoxMemory::buttonToString(Button b)
{
  using BB = QDialogButtonBox;

  static const std::map<Button, QString> map = {
      {BB::NoButton, "none"},
      {BB::Ok, "ok"},
      {BB::Save, "save"},
      {BB::SaveAll, "saveall"},
      {BB::Open, "open"},
      {BB::Yes, "yes"},
      {BB::YesToAll, "yestoall"},
      {BB::No, "no"},
      {BB::NoToAll, "notoall"},
      {BB::Abort, "abort"},
      {BB::Retry, "retry"},
      {BB::Ignore, "ignore"},
      {BB::Close, "close"},
      {BB::Cancel, "cancel"},
      {BB::Discard, "discard"},
      {BB::Help, "help"},
      {BB::Apply, "apply"},
      {BB::Reset, "reset"},
      {BB::RestoreDefaults, "restoredefaults"}};

  auto itor = map.find(b);

  if (itor == map.end()) {
    return QString("0x%1").arg(static_cast<int>(b), 0, 16);
  } else {
    return QString("'%1' (0x%2)").arg(itor->second).arg(static_cast<int>(b), 0, 16);
  }
}

}  // namespace MOBase