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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/***************************************************************************
* ktouchlectureeditor.cpp *
* ----------------------- *
* Copyright (C) 2004 by Andreas Nicolai *
* ghorwin@users.sourceforge.net *
* *
* This program 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. *
***************************************************************************/
#include <tqlabel.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <kpushbutton.h>
#include <klistview.h>
#include <klineedit.h>
#include <ktextedit.h>
#include <klocale.h>
#include <kdebug.h>
#include <ksqueezedtextlabel.h>
#include <kmessagebox.h>
#include <kcombobox.h>
#include <kfontdialog.h>
#include <tdefiledialog.h>
#include <algorithm> // for std::swap
#include "ktouchlectureeditor.h"
#include "ktouchlectureeditor.moc"
#include "ktouchlecture.h"
#include "ktouchopenrequest.h"
#include "ktouch.h"
// **************************
// ***** Public functions ***
// **************************
KTouchLectureEditor::KTouchLectureEditor(TQWidget *parent, const char* name, bool modal, WFlags fl)
: KTouchLectureEditorDlg(parent, name, modal, fl)
{
levelListView->setSorting(-1); // don't sort my level list view!
connect(levelListView, TQT_SIGNAL(selectionChanged(TQListViewItem*)),this, TQT_SLOT(newSelection(TQListViewItem*)) );
connect(newCharsEdit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(newCharsChanged(const TQString&)) );
connect(newBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(newLevel()) );
connect(deleteBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(deleteLevel()) );
connect(upBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(moveUp()) );
connect(downBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(moveDown()) );
// make the connections for making the lecture modified
connect(titleEdit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(setModified()) );
connect(lectureCommentEdit, TQT_SIGNAL(textChanged()), this, TQT_SLOT(setModified()) );
connect(levelCommentEdit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(setModified()) );
connect(linesTextEdit, TQT_SIGNAL(textChanged()), this, TQT_SLOT(setModified()) );
// The font, open, save, saveas and close buttons are already connected
}
// -----------------------------------------------------------------------------
bool KTouchLectureEditor::startEditor(const KURL& url) {
// call open request dialog and load a lecture, and start the dialogs event loop if
// the user did not cancel the open request dialog
if (openLectureFile(url)==TQDialog::Accepted) {
exec();
return true;
}
else return false;
}
// -----------------------------------------------------------------------------
// *************************
// ***** Private slots *****
// *************************
void KTouchLectureEditor::fontBtnClicked() {
TQFont f(m_lecture.m_fontSuggestions);
if (KFontDialog::getFont(f, false, this, true)==TQDialog::Accepted) {
linesTextEdit->setFont(f);
lectureCommentEdit->setFont(f);
levelCommentEdit->setFont(f);
newCharsEdit->setFont(f);
titleEdit->setFont(f);
levelListView->setFont(f);
m_lecture.m_fontSuggestions = f.family();
setModified();
}
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::openBtnClicked() {
saveModified(); // save if modified
openLectureFile("");
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::saveBtnClicked() {
if (m_currentURL.isEmpty()) saveAsBtnClicked();
else {
transfer_from_dialog();
m_lecture.saveXML(this, m_currentURL);
setModified(false);
}
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::saveAsBtnClicked() {
TQString tmp = KFileDialog::getSaveFileName(TQString(),
"*.ktouch.xml|KTouch Lecture Files(*.ktouch.xml)\n*.*|All Files", this, i18n("Save Training Lecture") );
if (!tmp.isEmpty()) {
transfer_from_dialog();
m_currentURL = tmp;
m_lecture.saveXML(this, m_currentURL);
setCaption(m_currentURL.url());
setModified(false);
}
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::newSelection(TQListViewItem* item) {
if (m_selecting) return;
bool current_modified_flag = m_modified; // remember our current status
// first store the current level data
storeCurrentLevel();
// now we need to find the level which has been selected
TQListViewItem *i = levelListView->firstChild();
unsigned int level=0;
while (i!=0 && i!=item) {
i = i->nextSibling();
++level;
};
if (i!=0) {
m_currentItem = i;
m_level = level;
showCurrentLevel();
}
m_selecting = true; // prevent the selectionChanged() signal from interfering
levelListView->setSelected(m_currentItem, true);
m_selecting = false;
if (m_lecture.m_lectureData.size()==1) {
downBtn->setEnabled(false);
upBtn->setEnabled(false);
}
else {
if (m_level==m_lecture.m_lectureData.size()-1) downBtn->setEnabled(false);
else downBtn->setEnabled(true);
if (m_level==0) upBtn->setEnabled(false);
else upBtn->setEnabled(true);
};
setModified(current_modified_flag);
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::newCharsChanged(const TQString& text) {
if (m_currentItem==0) return; // shouldn't happen, but a little bit of paranoia...
m_currentItem->setText(0, text);
setModified();
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::newLevel() {
createNewLevel();
TQListViewItem *newItem = new TQListViewItem( levelListView,
levelListView->lastItem(), m_lecture.m_lectureData.back().m_newChars );
newSelection(newItem);
upBtn->setEnabled(true);
downBtn->setEnabled(false);
deleteBtn->setEnabled(true);
setModified();
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::deleteLevel() {
// Open for discussion: Should there be a message box to confirm the delete?
if (m_level >= m_lecture.m_lectureData.size()) return; // paranoia check
m_selecting = true; // prevent the selectionChanged() signal from interfering
// first remove the item from the list view
delete m_currentItem;
// then remove the level data
TQValueVector<KTouchLevelData>::iterator it=m_lecture.m_lectureData.begin();
std::advance(it, m_level);
m_lecture.m_lectureData.erase(it);
m_currentItem = levelListView->firstChild();
for (unsigned int i=0; i<m_level; ++i)
m_currentItem = m_currentItem->nextSibling();
levelListView->setSelected(m_currentItem, true);
showCurrentLevel();
m_selecting = false;
if (m_lecture.m_lectureData.size()==1)
deleteBtn->setEnabled(false);
if (m_level==m_lecture.m_lectureData.size()-1)
downBtn->setEnabled(false);
if (m_level==0)
upBtn->setEnabled(false);
setModified();
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::moveUp() {
if (m_level==0) return;
m_selecting=true; // again, I don't want to process changeSelection() signals now
storeCurrentLevel();
TQListViewItem *upperItem = m_currentItem->itemAbove();
std::swap(m_lecture.m_lectureData[m_level], m_lecture.m_lectureData[m_level-1]);
upperItem->setText(0, m_lecture.m_lectureData[m_level-1].m_newChars);
m_currentItem->setText(0, m_lecture.m_lectureData[m_level].m_newChars);
m_currentItem=upperItem;
--m_level;
levelListView->setSelected(m_currentItem, true);
showCurrentLevel();
m_selecting = false;
if (m_level==0)
upBtn->setEnabled(false);
downBtn->setEnabled(true);
setModified();
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::moveDown() {
if (m_level>=m_lecture.m_lectureData.size()-1) return;
m_selecting=true; // again, I don't want to process changeSelection() signals now
storeCurrentLevel();
TQListViewItem *lowerItem = m_currentItem->itemBelow();
std::swap(m_lecture.m_lectureData[m_level], m_lecture.m_lectureData[m_level+1]);
m_currentItem->setText(0, m_lecture.m_lectureData[m_level].m_newChars);
lowerItem->setText(0, m_lecture.m_lectureData[m_level+1].m_newChars);
m_currentItem=lowerItem;
++m_level;
levelListView->setSelected(m_currentItem, true);
showCurrentLevel();
m_selecting = false;
if (m_level==m_lecture.m_lectureData.size()-1)
downBtn->setEnabled(false);
upBtn->setEnabled(true);
setModified();
}
// -----------------------------------------------------------------------------
// ****************************
// ***** Private functions ****
// ****************************
void KTouchLectureEditor::transfer_to_dialog() {
bool current_modified_flag = m_modified;
// set the title and the filename of the lecture
titleEdit->setText(m_lecture.title());
lectureCommentEdit->setText(m_lecture.m_comment);
if (m_currentURL.isEmpty()) setCaption(i18n("KTouch Lecture Editor - ") + i18n("<new unnamed lecture file>"));
else setCaption(i18n("KTouch Lecture Editor - ") + m_currentURL.fileName());
// copy the 'new char' strings of the lectures into the list view
levelListView->clear();
TQValueVector<KTouchLevelData>::const_iterator it=m_lecture.m_lectureData.begin();
// add first item
TQListViewItem *item=new TQListViewItem( levelListView, (it++)->m_newChars );
// add all the others
for (;it!=m_lecture.m_lectureData.end(); ++it)
item = new TQListViewItem( levelListView, item, it->m_newChars );
m_currentItem=levelListView->firstChild();
m_selecting = true; // prevent the selectionChanged() signal from interfering
levelListView->setSelected(m_currentItem, true);
m_selecting = false;
// open first lesson
m_level=0;
showCurrentLevel();
// set up the buttons
upBtn->setEnabled(false);
if (m_lecture.m_lectureData.size()>1) {
downBtn->setEnabled(true);
deleteBtn->setEnabled(true);
}
else {
downBtn->setEnabled(false);
deleteBtn->setEnabled(false);
}
// finally the font
if (!m_lecture.m_fontSuggestions.isEmpty()) {
TQFont f("Monospace");
// TODO : multiple font suggestions
f.fromString(m_lecture.m_fontSuggestions);
linesTextEdit->setFont(f);
lectureCommentEdit->setFont(f);
levelCommentEdit->setFont(f);
newCharsEdit->setFont(f);
titleEdit->setFont(f);
levelListView->setFont(f);
}
setModified(current_modified_flag);
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::transfer_from_dialog() {
storeCurrentLevel();
m_lecture.m_title = titleEdit->text();
m_lecture.m_comment = lectureCommentEdit->text();
setModified();
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::showCurrentLevel() {
if (m_level >= m_lecture.m_lectureData.size()) return; // should not happen, but why running a risk here...
levelLabel->setText(i18n("Data of Level %1").arg(m_level+1) );
levelCommentEdit->setText(m_lecture.m_lectureData[m_level].m_comment);
newCharsEdit->setText(m_lecture.m_lectureData[m_level].m_newChars);
TQString text;
for (TQValueVector<TQString>::const_iterator it=m_lecture.m_lectureData[m_level].m_lines.begin();
it!=m_lecture.m_lectureData[m_level].m_lines.end(); ++it)
{
text += *it + '\n';
}
linesTextEdit->setText(text);
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::storeCurrentLevel() {
if (m_level>=m_lecture.m_lectureData.size()) return; // should not happen, but ... better check one time too much...
m_lecture.m_lectureData[m_level].m_comment = levelCommentEdit->text();
m_lecture.m_lectureData[m_level].m_newChars = newCharsEdit->text();
m_lecture.m_lectureData[m_level].m_lines.clear();
TQString text = linesTextEdit->text();
TQStringList lines;
TQString currentLine;
for (unsigned int i=0; i<text.length(); ++i) {
TQChar c = text[i];
if (c=='\t') c=' '; // replace tabs with spaces
if (c=='\n') {
lines.append(currentLine);
currentLine = "";
}
else
currentLine += c;
};
lines.append(currentLine);
for (TQStringList::const_iterator it=lines.begin(); it!=lines.end(); ++it) {
if ((*it).isEmpty()) continue;
m_lecture.m_lectureData[m_level].m_lines.push_back(*it);
}
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::createNewLevel() {
KTouchLevelData newLevel;
newLevel.m_newChars = i18n("abcdefghijklmnopqrstuvwxyz");
newLevel.m_comment = TQString();
newLevel.m_lines.clear(); // remove the lines of the default mini level
newLevel.m_lines.push_back(i18n("Enter your lines here..."));
m_lecture.m_lectureData.push_back(newLevel);
}
// -----------------------------------------------------------------------------
int KTouchLectureEditor::openLectureFile(const KURL& url) {
// First setup the open request dialog
KTouchOpenRequest dlg(this);
// Call the dialog
KURL new_url;
int result = dlg.requestFileToOpen(new_url,
i18n("Open Lecture File"),
i18n("Which Lecture File Would You Like to Edit?"),
i18n("Edit current lecture:"),
i18n("Open a default lecture:"),
i18n("Open a lecture file:"),
i18n("Create new lecture"),
url, KTouchPtr->lectureFiles(), i18n("<no lecture files available>"));
if (result == TQDialog::Accepted) {
// Ok, user confirmed the dialog, now lets get the url
m_currentURL = new_url;
// Try to load the lecture, if that failes, we create a new lecture instead
m_lecture = KTouchLecture(); // empty the lecture
if (!m_currentURL.isEmpty()) {
// try to read old format first then XML format
if (!m_lecture.load(this, m_currentURL) && !m_lecture.loadXML(this, m_currentURL)) {
KMessageBox::sorry(this, i18n("Could not open the lecture file, creating a new one instead."));
m_currentURL = TQString(); // new lectures haven't got a URL
}
}
// If we have no URL, we create a new lecture - can happen if either the user
// chose "new lecture" or the chosen lecture could not be opened
if (m_currentURL.isEmpty()) {
m_lecture.createDefault();
m_modified = true; // new lectures are modified by default
}
else
m_modified = false; // newly read lectures are not modified in the begin
// Update our editor with the lecture data
transfer_to_dialog();
return TQDialog::Accepted;
}
else return TQDialog::Rejected;
}
// -----------------------------------------------------------------------------
void KTouchLectureEditor::setModified(bool flag) {
m_modified = flag;
if (!m_currentURL.isEmpty()) {
if (flag) setCaption(i18n("KTouch Lecture Editor - ") + m_currentURL.fileName() + i18n(" (modified)"));
else setCaption(i18n("KTouch Lecture Editor - ") + m_currentURL.fileName());
}
}
// -----------------------------------------------------------------------------
bool KTouchLectureEditor::saveModified() {
if (!m_modified) return true;
// ok, ask the user to save the changes
int result = KMessageBox::questionYesNoCancel(this,
i18n("The lecture has been changed. Do you want to save the changes?"),TQString(),KStdGuiItem::save(),KStdGuiItem::discard());
if (result == KMessageBox::Cancel) return false; // User aborted
if (result == KMessageBox::Yes) saveBtnClicked();
// if successfully saved the modified flag will be resetted in the saveBtnClicked() function
return true; // User acknowledged
}
// -----------------------------------------------------------------------------
|