This repository was archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathappsettings.cpp
155 lines (142 loc) · 4.84 KB
/
appsettings.cpp
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
#include "appsettings.h"
#include "ui_appsettings.h"
#include <QMessageBox>
AppSettings::AppSettings(QWidget *parent) :
QDialog(parent),
ui(new Ui::AppSettings)
{
ui->setupUi(this);
}
void AppSettings::setupUI(ScreenCapSettings *settings) {
ui->txtImageDimension->setText(QString::number(settings->GetCapDimensions()));
ui->chkMinimizeToTray->setChecked(settings->GetMinimizeToTray());
ui->chkStartCapturing->setChecked(settings->GetCaptureOnStartup());
ui->chkStartMinimized->setChecked(settings->GetStartMinimized());
this->addItemsToFormatDdl(ui->ddlImgFormat, settings);
this->addItemsToQualityDdl(ui->ddlQuality, settings);
}
/**
* @brief AppSettings::GetUpdatedSettings
* Fetches the updated settings when the user presses the OK button
* @param settings Current application settings.
* @return Updated application settings.
*/
ScreenCapSettings* AppSettings::GetUpdatedSettings(ScreenCapSettings *settings) {
settings->SetCapDimensions(ui->txtImageDimension->text().toInt());
settings->SetCaptureOnStartup(ui->chkStartCapturing->isChecked());
settings->SetMinimizeToTray(ui->chkMinimizeToTray->isChecked());
settings->SetStartMinimized(ui->chkStartMinimized->isChecked());
int imgQuality = ui->ddlQuality->itemData(ui->ddlQuality->currentIndex()).toInt();
int imgFormat = ui->ddlImgFormat->itemData(ui->ddlImgFormat->currentIndex()).toInt();
settings->SetImgFormat(imgFormat);
settings->SetImgQuality(imgQuality);
return settings;
}
/**
* @brief AppSettings::addItemsToQualityDdl
* Adds items to the quality dropdown and also selects the
* currently preferred image quality.
* @param cmbBox Image quality combobox
* @param settings Application settings
*/
void AppSettings::addItemsToQualityDdl(QComboBox *cmbBox, ScreenCapSettings *settings)
{
QMap<int, QString> qQualities = settings->GetListOfImgQualities();
QMap<int, QString>::iterator i;
int currQuality = settings->GetImgQuality();
int intIndex = 0;
int currIndex = -1;
for(i = qQualities.begin(); i != qQualities.end(); ++i) {
cmbBox->addItem(i.value(), QVariant(i.key()));
if(i.key() == currQuality) {
currIndex = intIndex;
}
++intIndex;
}
cmbBox->setCurrentIndex(currIndex);
}
/**
* @brief AppSettings::addItemsToFormatDdl
* Adds items to the image format dropdown and also selects the
* default image
* @param cmbBox Image format combobox
* @param settings Application settings.
*/
void AppSettings::addItemsToFormatDdl(QComboBox *cmbBox, ScreenCapSettings *settings)
{
QMap<int, QString> qFormats = settings->GetListOfImgFormats();
QMap<int, QString>::iterator i;
int intIndex = 0;
int currIndex = -1;
int currFormat = settings->GetImgFormatInt();
for(i = qFormats.begin(); i != qFormats.end(); ++i) {
cmbBox->addItem(i.value(), QVariant(i.key()));
if(currFormat == i.key()) {
currIndex = intIndex;
}
++intIndex;
}
cmbBox->setCurrentIndex(currIndex);
}
/**
* @brief AppSettings::done
* Overriding the base done method to perform validations.
* If everything is okay, calls the base QDialog::done()
* to close the dialog.
* @param result QDialog::result()
*/
void AppSettings::done(int result) {
if(QDialog::Accepted == result) {
if(this->checkSettings()) {
QDialog::done(result);
} else {
return;
}
} else {
QDialog::done(result);
return;
}
}
/**
* @brief AppSettings::checkSettings
* Performs validation on the values entered by the user.
* @return true if everything is okay, else false.
*/
bool AppSettings::checkSettings() {
// Validating the dimensions...
bool isDimensionInt = false;
QString strDimensions = ui->txtImageDimension->text().trimmed();
int intDimensions = strDimensions.toInt(&isDimensionInt);
if(strDimensions.length() != 0 || isDimensionInt) {
if(intDimensions > MIN_DIMENSION_HEIGHT && intDimensions <= 100) {
return true;
}
this->dimensionError();
return false;
}
this->dimensionError();
return false;
}
/**
* @brief AppSettings::dimensionError
* Clears the textbox and displays a message box
* when the user enters an incorrect dimension.
*/
void AppSettings::dimensionError() {
ui->txtImageDimension->setFocus();
ui->txtImageDimension->setText("");
QMessageBox msgBox;
QFont font(DLG_FONT_FAMILY);
font.setStyleHint(QFont::TypeWriter);
msgBox.setWindowTitle("Settings Error");
msgBox.setIcon(QMessageBox::Information);
msgBox.setText("The dimensions should be between " + QString::number(MIN_DIMENSION_HEIGHT) + " and 100.");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setFont(font);
msgBox.exec();
}
AppSettings::~AppSettings()
{
delete ui;
}