This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.cpp
255 lines (203 loc) · 6.34 KB
/
helper.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
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
246
247
248
249
250
251
252
253
254
255
#include "helper.h"
#include "mainwindow.h"
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>
#include <QtGui/QApplication>
#include <QtCore/QSettings>
#include <QtCore/QRegExp>
#include <QtCore/QTextStream>
Helper::Helper(MainWindow *mainWindow)
: QObject(0)
{
mMainWindow = mainWindow;
toggleFullScreen();
}
Helper::Helper(const Helper ©)
: QObject(copy.parent()), mMainWindow(0)
{
mSpeechTitle = copy.mSpeechTitle;
mSpeechItems = copy.mSpeechItems;
mSpeechTitles = copy.mSpeechTitles;
mMainWindow = copy.mMainWindow;
}
QString Helper::speechText() const
{
if (mSpeechTitle.isEmpty()) {
return QString();
}
return mSpeechItems[mSpeechTitle];
}
void Helper::setSpeechText(const QString &speechText)
{
if(mSpeechTitle.isEmpty()) {
qDebug() << "!!! setSpeechText called and mSpeechTitle.isEmpty()";
return;
}
if (speechText == mSpeechItems[mSpeechTitle]) {
return;
}
mSpeechItems[mSpeechTitle] = speechText;
QSettings settings;
settings.beginGroup("Main");
settings.setValue("lastUsedSpeechText", mSpeechItems[mSpeechTitle]);
settings.endGroup();
settings.sync();
emit speechTextChanged();
}
QString Helper::speechTitle() const
{
return mSpeechTitle;
}
void Helper::setSpeechTitle(const QString &speechTitle)
{
if (!mSpeechItems.contains(speechTitle)) {
qDebug() << "!!! setSpeechTitle called and !mSpeechItems.contains(speechTitle)";
return;
}
QString oldSpeechText = speechText();
mSpeechTitle = speechTitle;
QSettings settings;
settings.beginGroup("Main");
settings.setValue("lastUsedSpeechTitle", mSpeechTitle);
settings.setValue("lastUsedSpeechText", mSpeechItems[mSpeechTitle]);
settings.endGroup();
settings.sync();
emit speechTitleChanged();
if (oldSpeechText != mSpeechItems[mSpeechTitle]) {
emit speechTextChanged();
}
}
void Helper::changeSpeechTitle(const QString &speechTitle)
{
QString oldTitle = mSpeechTitle;
mSpeechItems[speechTitle] = mSpeechItems[mSpeechTitle];
mSpeechItems.remove(oldTitle);
int pos = mSpeechTitles.indexOf(oldTitle);
if (pos == -1) {
mSpeechTitles.append(speechTitle);
} else {
mSpeechTitles.replace(pos, speechTitle);
}
mSpeechTitle = speechTitle;
emit speechTitleChanged();
emit speechItemsChanged();
}
void Helper::toggleFullScreen()
{
if (!mMainWindow->isFullScreen()) {
mMainWindow->showFullScreen();
} else {
mMainWindow->showNormal();
}
}
void Helper::openSpeechDialog(const QString &tryThisFirst)
{
QString lastUsedSpeechPath = tryThisFirst;
QFile file(lastUsedSpeechPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
lastUsedSpeechPath = QFileDialog::getOpenFileName(mMainWindow,
tr("Open Speech"), lastUsedSpeechPath);
file.setFileName(lastUsedSpeechPath);
while(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox messageBox(mMainWindow);
messageBox.setText(tr("Sorry, the file couldn't be opened for read"));
messageBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok);
int ret = messageBox.exec();
if (ret == QMessageBox::Cancel) {
qDebug() << "User cancelled opening the file";
return;
}
lastUsedSpeechPath = QFileDialog::getOpenFileName(mMainWindow,
tr("Open Speech"), "");
file.setFileName(lastUsedSpeechPath);
}
}
QSettings settings;
settings.beginGroup("Main");
settings.setValue("lastUsedSpeechPath", lastUsedSpeechPath);
settings.endGroup();
settings.sync();
// erase all!
mSpeechItems = QHash<QString, QString>();
mSpeechTitles = QStringList();
mSpeechTitle = QString();
QString key, value;
while (!file.atEnd()) {
QString line = QString::fromUtf8(file.readLine());
if (line.startsWith("=")) {
if (!value.isEmpty() && !key.isEmpty()) {
line = line.replace("=", "").trimmed();
mSpeechItems[key] = value;
mSpeechTitles.append(key);
if (mSpeechTitle.isEmpty()) {
setSpeechTitle(key);
}
}
key = line.replace("=", "").trimmed();
value = QString();
} else {
value += line;
}
}
if (!value.isEmpty() && !key.isEmpty()) {
mSpeechItems[key] = value;
mSpeechTitles.append(key);
if (mSpeechTitle.isEmpty()) {
setSpeechTitle(key);
}
}
emit speechItemsChanged();
}
void Helper::deleteSpeechItem(const QString &speechTitle)
{
if (!mSpeechItems.contains(speechTitle) || mSpeechTitles.count() == 1) {
return;
}
int titlesIndex = mSpeechTitles.indexOf(speechTitle);
mSpeechItems.remove(speechTitle);
mSpeechTitles.removeOne(speechTitle);
if (mSpeechTitle == speechTitle) {
int nextIndex;
if (titlesIndex == 0) {
nextIndex = titlesIndex;
} else {
nextIndex = titlesIndex - 1;
}
QString nextTitle = mSpeechTitles.at(nextIndex) ;
setSpeechTitle(nextTitle);
}
emit speechItemsChanged();
}
void Helper::save()
{
qDebug() << "file saved!";
QSettings settings;
settings.beginGroup("Main");
QString lastUsedSpeechPath = settings.value("lastUsedSpeechPath", "").toString();
settings.endGroup();
QFile file(lastUsedSpeechPath);
while(!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
lastUsedSpeechPath = QFileDialog::getSaveFileName(mMainWindow,
tr("Save Speech"), lastUsedSpeechPath);
file.setFileName(lastUsedSpeechPath);
}
settings.setValue("lastUsedSpeechPath", lastUsedSpeechPath);
settings.endGroup();
settings.sync();
QString text;
Q_FOREACH(QString title, mSpeechTitles) {
text += "== " + title + " ==\n" + mSpeechItems[title];
}
QTextStream out(&file);
out << text;
file.close();
qDebug() << "file saved!";
}
void Helper::appendSpeech(const QString &title, const QString &text)
{
mSpeechItems[title] = text;
mSpeechTitles.append(title);
emit speechItemsChanged();
}