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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
/***************************************************************************
schemaeditor.cpp - description
-------------------
begin : mar apr 17 16:44:59 CEST 2001
copyright : (C) 2001 by Andrea Rizzi
email : rizzi@kde.org
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#define TABLE_COLORS 20
#include "schemaeditor.h"
#include "schemaeditor.moc"
#include <dcopclient.h>
#include <tqlabel.h>
#include <tqlineedit.h>
#include <tqwmatrix.h>
#include <tqcombobox.h>
#include <kdebug.h>
#include <tqcheckbox.h>
#include <kstandarddirs.h>
//#include <errno.h>
#include <tqslider.h>
#include <klocale.h>
#include <kfiledialog.h>
#include <kinputdialog.h>
#include <tqtoolbutton.h>
#include <kmessagebox.h>
#include <ksharedpixmap.h>
#include <kimageeffect.h>
#include <tqimage.h>
// SchemaListBoxText is a list box text item with schema filename
class SchemaListBoxText : public TQListBoxText
{
public:
SchemaListBoxText(const TQString &title, const TQString &filename): TQListBoxText(title)
{
m_filename = filename;
};
const TQString filename() { return m_filename; };
private:
TQString m_filename;
};
SchemaEditor::SchemaEditor(TQWidget * parent, const char *name)
:SchemaDialog(parent, name)
{
schMod= false;
loaded = false;
schemaLoaded = false;
change = false;
oldSlot = 0;
oldSchema = -1;
color.resize(20);
type.resize(20);
bold.resize(20);
transparent.resize(20);
defaultSchema = "";
spix = new KSharedPixmap;
connect(spix, TQT_SIGNAL(done(bool)), TQT_SLOT(previewLoaded(bool)));
DCOPClient *client = kapp->dcopClient();
if (!client->isAttached())
client->attach();
TQByteArray data;
TQDataStream args(data, IO_WriteOnly);
args << 1;
client->send("kdesktop", "KBackgroundIface", "setExport(int)", data);
transparencyCheck->setChecked(true);
transparencyCheck->setChecked(false);
KGlobal::locale()->insertCatalogue("konsole"); // For schema translations
connect(imageBrowse, TQT_SIGNAL(clicked()), this, TQT_SLOT(imageSelect()));
connect(saveButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(saveCurrent()));
connect(removeButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(removeCurrent()));
connect(colorCombo, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotColorChanged(int)));
connect(typeCombo, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotTypeChanged(int)));
connect(schemaList, TQT_SIGNAL(highlighted(int)), this, TQT_SLOT(readSchema(int)));
connect(shadeColor, TQT_SIGNAL(changed(const TQColor&)), this, TQT_SLOT(updatePreview()));
connect(shadeSlide, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(updatePreview()));
connect(transparencyCheck, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(updatePreview()));
connect(backgndLine, TQT_SIGNAL(returnPressed()), this, TQT_SLOT(updatePreview()));
connect(titleLine, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(schemaModified()));
connect(shadeColor, TQT_SIGNAL(changed(const TQColor&)), this, TQT_SLOT(schemaModified()));
connect(shadeSlide, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(schemaModified()));
connect(transparencyCheck, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(schemaModified()));
connect(modeCombo, TQT_SIGNAL(activated(int)), this, TQT_SLOT(schemaModified()));
connect(backgndLine, TQT_SIGNAL(returnPressed()), this, TQT_SLOT(schemaModified()));
connect(transparentCheck, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(schemaModified()));
connect(boldCheck, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(schemaModified()));
connect(colorButton, TQT_SIGNAL(changed(const TQColor&)), this, TQT_SLOT(schemaModified()));
connect(backgndLine, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(schemaModified()));
connect(defaultSchemaCB, TQT_SIGNAL(toggled(bool)), this, TQT_SIGNAL(changed()));
removeButton->setEnabled( schemaList->currentItem() );
}
TQString SchemaEditor::schema()
{
TQString filename = defaultSchema;
int i = schemaList->currentItem();
if (defaultSchemaCB->isChecked() && i>=0)
filename = ((SchemaListBoxText *) schemaList->item(i))->filename();
return filename.section('/',-1);
}
void SchemaEditor::setSchema(TQString sch)
{
defaultSchema = sch;
sch = locate("data", "konsole/"+sch);
int sc = -1;
for (int i = 0; i < (int) schemaList->count(); i++)
if (sch == ((SchemaListBoxText *) schemaList->item(i))->filename())
sc = i;
oldSchema = sc;
if (sc == -1)
sc = 0;
schemaList->setCurrentItem(sc);
// readSchema(sc);
}
SchemaEditor::~SchemaEditor()
{
delete spix;
}
void SchemaEditor::updatePreview()
{
if (transparencyCheck->isChecked()) {
if (loaded) {
float rx = (100.0 - shadeSlide->value()) / 100;
TQImage ima(pix.convertToImage());
ima = KImageEffect::fade(ima, rx, shadeColor->color());
TQPixmap pm;
pm.convertFromImage(ima);
previewPixmap->setPixmap(pm);
previewPixmap->setScaledContents(true);
}
else //try to reload
{
if(!spix->loadFromShared(TQString("DESKTOP1")))
kdDebug(0) << "cannot load" << endl;
}
} else {
TQPixmap pm;
pm.load(backgndLine->text());
if ( pm.isNull() ) {
previewPixmap->clear();
} else {
previewPixmap->setPixmap(pm);
previewPixmap->setScaledContents(true);
}
}
}
void SchemaEditor::previewLoaded(bool l)
{
if (l) {
TQWMatrix mat;
pix =
spix->xForm(mat.
scale(180.0 / spix->TQPixmap::width(),
100.0 / spix->TQPixmap::height()));
kdDebug(0) << "Loaded" << endl;
loaded = true;
if (transparencyCheck->isChecked()) {
updatePreview();
}
} else
kdDebug(0) << "error loading" << endl;
}
void SchemaEditor::getList()
{
if (! schemaLoaded) {
loadAllSchema();
setSchema(defaultSchema);
schemaLoaded = true;
change = true;
}
}
void SchemaEditor::show()
{
getList();
SchemaDialog::show();
}
void SchemaEditor::loadAllSchema(TQString currentFile)
{
TQStringList list = KGlobal::dirs()->findAllResources("data", "konsole/*.schema");
TQStringList::ConstIterator it;
disconnect(schemaList, TQT_SIGNAL(highlighted(int)), this, TQT_SLOT(readSchema(int)));
schemaList->clear();
TQListBoxItem* currentItem = 0;
for (it = list.begin(); it != list.end(); ++it) {
TQString name = (*it);
TQString title = readSchemaTitle(name);
// Only insert new items so that local items override global
if (schemaList->tqfindItem(title, ExactMatch) == 0) {
if (title.isNull() || title.isEmpty())
title=i18n("untitled");
schemaList->insertItem(new SchemaListBoxText(title, name));
if (currentFile==name.section('/',-1))
currentItem = schemaList->item( schemaList->count()-1 );
}
}
schemaList->sort();
schemaList->setCurrentItem(0); // select the first added item correctly too
schemaList->setCurrentItem(currentItem);
connect(schemaList, TQT_SIGNAL(highlighted(int)), this, TQT_SLOT(readSchema(int)));
schemaListChanged();
}
void SchemaEditor::imageSelect()
{
TQString start;
start = backgndLine->text();
if (start.isEmpty())
{
TQStringList list=KGlobal::dirs()->resourceDirs("wallpaper");
if(list.count()>0)
start= list.last();
}
KURL url = KFileDialog::getImageOpenURL(start, 0, i18n("Select Background Image"));
// KURL url=KFileDialog::getOpenURL(start,"",0,i18n("Select Background Image"));
if(!url.path().isEmpty())
{
backgndLine->setText(url.path());
updatePreview();
}
}
void SchemaEditor::slotTypeChanged(int slot)
{
schemaModified();
bool active = slot == 0 || slot == 3;
colorButton->setEnabled(active);
boldCheck->setEnabled(active);
transparentCheck->setEnabled(active);
}
void SchemaEditor::slotColorChanged(int slot)
{
kdDebug(0) << slot << endl;
color[oldSlot] = colorButton->color();
type[oldSlot] = typeCombo->currentItem();
bold[oldSlot] = boldCheck->isChecked();
transparent[oldSlot] = transparentCheck->isChecked();
change = false; // Don't mark as modified
transparentCheck->setChecked(transparent[slot]);
boldCheck->setChecked(bold[slot]);
typeCombo->setCurrentItem(type[slot]);
colorButton->setColor(color[slot]);
oldSlot = slot;
change = true;
}
void SchemaEditor::removeCurrent()
{
int i = schemaList->currentItem();
if(i==-1)
return;
TQString base = ((SchemaListBoxText *) schemaList->item(i))->filename();
// Query if system schemas should be removed
if (locateLocal("data", "konsole/" + base.section('/', -1)) != base) {
int code = KMessageBox::warningContinueCancel(this,
i18n("You are trying to remove a system schema. Are you sure?"),
i18n("Removing System Schema"),
KGuiItem(i18n("&Delete"), "editdelete"));
if (code != KMessageBox::Continue)
return;
}
TQString base_filename = base.section('/',-1);
if(base_filename==schema())
setSchema("");
if (!TQFile::remove(base))
KMessageBox::error(this,
i18n("Cannot remove the schema.\nMaybe it is a system schema.\n"),
i18n("Error Removing Schema"));
loadAllSchema();
setSchema(defaultSchema);
}
void SchemaEditor::saveCurrent()
{
//This is to update the color table
colorCombo->setCurrentItem(0);
slotColorChanged(0);
TQString fullpath;
if (schemaList->currentText() == titleLine->text()) {
int i = schemaList->currentItem();
fullpath = ((SchemaListBoxText *) schemaList->item(i))->filename().section('/',-1);
}
else {
// Only ask for a name for changed titleLine, considered a "save as"
fullpath = titleLine->text().stripWhiteSpace().simplifyWhiteSpace()+".schema";
bool ok;
fullpath = KInputDialog::getText( i18n( "Save Schema" ),
i18n( "File name:" ), fullpath, &ok, this );
if (!ok) return;
}
if (fullpath[0] != '/')
fullpath = KGlobal::dirs()->saveLocation("data", "konsole/") + fullpath;
TQFile f(fullpath);
if (f.open(IO_WriteOnly)) {
TQTextStream t(&f);
t.setEncoding( TQTextStream::UnicodeUTF8 );
t << "# schema for konsole autogenerated with the schema editor" << endl;
t << endl;
t << "title " << titleLine->text() << endl; // Use title line as schema title
t << endl;
if (transparencyCheck->isChecked()) {
TQColor c = shadeColor->color();
TQString tra;
tra.sprintf("transparency %1.2f %3d %3d %3d",
1.0 * (100 - shadeSlide->value()) / 100, c.red(), c.green(), c.blue());
t << tra << endl;
}
if (!backgndLine->text().isEmpty()) {
TQString smode;
int mode;
mode = modeCombo->currentItem();
if (mode == 0)
smode = "tile";
if (mode == 1)
smode = "center";
if (mode == 2)
smode = "full";
TQString image;
image.sprintf("image %s %s",
(const char *) smode.latin1(),
(const char *) backgndLine->text().utf8());
t << image << endl;
}
t << endl;
t << "# foreground colors" << endl;
t << endl;
t << "# note that the default background color is flagged" << endl;
t << "# to become transparent when an image is present." << endl;
t << endl;
t << "# slot transparent bold" << endl;
t << "# | red grn blu | |" << endl;
t << "# V V--color--V V V" << endl;
for (int i = 0; i < 20; i++) {
TQString scol;
if (type[i] == 0)
scol.sprintf("color %2d %3d %3d %3d %2d %1d # %s", i,
color[i].red(), color[i].green(), color[i].blue(),
transparent[i], bold[i],
(const char *) colorCombo->text(i).utf8());
else if (type[i] == 1)
scol.sprintf("sysfg %2d %2d %1d # %s", i,
transparent[i], bold[i],
(const char *) colorCombo->text(i).utf8());
else if (type[i] == 2)
scol.sprintf("sysbg %2d %2d %1d # %s", i,
transparent[i], bold[i],
(const char *) colorCombo->text(i).utf8());
else {
int ch, cs, cv;
color[i].hsv(&ch, &cs, &cv);
scol.sprintf("rcolor %1d %3d %3d %2d %1d # %s", i,
cs, cv, transparent[i], bold[i],
(const char *) colorCombo->text(i).utf8());
}
t << scol << endl;
}
f.close();
} else
KMessageBox::error(this, i18n("Cannot save the schema.\nMaybe permission denied.\n"),
i18n("Error Saving Schema"));
schMod=false;
loadAllSchema(fullpath.section('/',-1));
}
void SchemaEditor::schemaModified()
{
if (change) {
saveButton->setEnabled(titleLine->text().length() != 0);
schMod=true;
emit changed();
}
}
TQString SchemaEditor::readSchemaTitle(const TQString & file)
{
/*
Code taken from konsole/konsole/schema.cpp
*/
TQString fPath = locate("data", "konsole/" + file);
if (fPath.isNull())
fPath = locate("data", file);
if (fPath.isNull())
return 0;
FILE *sysin = fopen(TQFile::encodeName(fPath), "r");
if (!sysin)
return 0;
char line[100];
while (fscanf(sysin, "%80[^\n]\n", line) > 0)
if (strlen(line) > 5)
if (!strncmp(line, "title", 5)) {
fclose(sysin);
return i18n(line + 6);
}
return 0;
}
void SchemaEditor::schemaListChanged()
{
TQStringList titles, filenames;
SchemaListBoxText *item;
for (int index = 0; index < (int) schemaList->count(); index++) {
item = (SchemaListBoxText *) schemaList->item(index);
titles.append(item->text());
filenames.append(item->filename().section('/', -1));
}
emit schemaListChanged(titles, filenames);
}
void SchemaEditor::querySave()
{
int result = KMessageBox::questionYesNo(this,
i18n("The schema has been modified.\n"
"Do you want to save the changes?"),
i18n("Schema Modified"),
KStdGuiItem::save(),
KStdGuiItem::discard());
if (result == KMessageBox::Yes)
{
saveCurrent();
}
}
void SchemaEditor::readSchema(int num)
{
/*
Code taken from konsole/konsole/schema.cpp
*/
if (oldSchema != -1) {
if (defaultSchemaCB->isChecked()) {
defaultSchema = ((SchemaListBoxText *) schemaList->item(oldSchema))->filename();
}
if(schMod) {
disconnect(schemaList, TQT_SIGNAL(highlighted(int)), this, TQT_SLOT(readSchema(int)));
schemaList->setCurrentItem(oldSchema);
querySave();
schemaList->setCurrentItem(num);
connect(schemaList, TQT_SIGNAL(highlighted(int)), this, TQT_SLOT(readSchema(int)));
schMod=false;
}
}
TQString fPath = locate("data", "konsole/" +
((SchemaListBoxText *) schemaList->item(num))->filename());
if (fPath.isNull())
fPath = locate("data",
((SchemaListBoxText *) schemaList->item(num))->filename());
if (fPath.isNull()) {
KMessageBox::error(this, i18n("Cannot find the schema."),
i18n("Error Loading Schema"));
return;
}
removeButton->setEnabled( TQFileInfo (fPath).isWritable () );
defaultSchemaCB->setChecked(fPath.section('/',-1) == defaultSchema.section('/',-1));
FILE *sysin = fopen(TQFile::encodeName(fPath), "r");
if (!sysin) {
KMessageBox::error(this, i18n("Cannot load the schema."),
i18n("Error Loading Schema"));
loadAllSchema();
return;
}
char line[100];
titleLine->setText(i18n("untitled"));
transparencyCheck->setChecked(false);
backgndLine->setText("");
while (fscanf(sysin, "%80[^\n]\n", line) > 0) {
if (strlen(line) > 5) {
if (!strncmp(line, "title", 5)) {
titleLine->setText(i18n(line + 6));
}
if (!strncmp(line, "image", 5)) {
char rend[100], path[100];
int attr = 1;
if (sscanf(line, "image %s %s", rend, path) != 2)
continue;
if (!strcmp(rend, "tile"))
attr = 2;
else if (!strcmp(rend, "center"))
attr = 3;
else if (!strcmp(rend, "full"))
attr = 4;
else
continue;
TQString qline(line);
backgndLine->setText(locate("wallpaper", qline.mid( qline.tqfind(" ",7)+1 ) ));
modeCombo->setCurrentItem(attr - 2);
}
if (!strncmp(line, "transparency", 12)) {
float rx;
int rr, rg, rb;
// Transparency needs 4 parameters: fade strength and the 3
// components of the fade color.
if (sscanf(line, "transparency %g %d %d %d", &rx, &rr, &rg, &rb) != 4)
continue;
transparencyCheck->setChecked(true);
shadeSlide->setValue((int)(100 - rx * 100));
shadeColor->setColor(TQColor(rr, rg, rb));
}
if (!strncmp(line,"rcolor",6)) {
int fi,ch,cs,cv,tr,bo;
if(sscanf(line,"rcolor %d %d %d %d %d",&fi,&cs,&cv,&tr,&bo) != 5)
continue;
if (!(0 <= fi && fi <= TABLE_COLORS))
continue;
ch = 0; // Random hue - set to zero
if (!(0 <= cs && cs <= 255 ))
continue;
if (!(0 <= cv && cv <= 255 ))
continue;
if (!(0 <= tr && tr <= 1 ))
continue;
if (!(0 <= bo && bo <= 1 ))
continue;
color[fi] = TQColor();
color[fi].setHsv(ch,cs,cv);
transparent[fi] = tr;
bold[fi] = bo;
type[fi] = 3;
}
if (!strncmp(line, "color", 5)) {
int fi, cr, cg, cb, tr, bo;
if (sscanf(line, "color %d %d %d %d %d %d", &fi, &cr, &cg, &cb, &tr, &bo) != 6)
continue;
if (!(0 <= fi && fi <= TABLE_COLORS))
continue;
if (!(0 <= cr && cr <= 255))
continue;
if (!(0 <= cg && cg <= 255))
continue;
if (!(0 <= cb && cb <= 255))
continue;
if (!(0 <= tr && tr <= 1))
continue;
if (!(0 <= bo && bo <= 1))
continue;
color[fi] = TQColor(cr, cg, cb);
transparent[fi] = tr;
bold[fi] = bo;
type[fi] = 0;
}
if (!strncmp(line, "sysfg", 5)) {
int fi, tr, bo;
if (sscanf(line, "sysfg %d %d %d", &fi, &tr, &bo) != 3)
continue;
if (!(0 <= fi && fi <= TABLE_COLORS))
continue;
if (!(0 <= tr && tr <= 1))
continue;
if (!(0 <= bo && bo <= 1))
continue;
color[fi] = kapp->tqpalette().active().text();
transparent[fi] = tr;
bold[fi] = bo;
type[fi] = 1;
}
if (!strncmp(line, "sysbg", 5)) {
int fi, tr, bo;
if (sscanf(line, "sysbg %d %d %d", &fi, &tr, &bo) != 3)
continue;
if (!(0 <= fi && fi <= TABLE_COLORS))
continue;
if (!(0 <= tr && tr <= 1))
continue;
if (!(0 <= bo && bo <= 1))
continue;
color[fi] = kapp->tqpalette().active().base();
transparent[fi] = tr;
bold[fi] = bo;
type[fi] = 2;
}
}
}
fclose(sysin);
int ii = colorCombo->currentItem();
transparentCheck->setChecked(transparent[ii]);
boldCheck->setChecked(bold[ii]);
typeCombo->setCurrentItem(type[ii]);
colorButton->setColor(color[ii]);
bool inactive = type[ii] == 1 || type[ii] == 2;
boldCheck->setDisabled(inactive);
transparentCheck->setDisabled(inactive);
colorButton->setDisabled(inactive);
oldSchema = num;
updatePreview();
schMod=false;
return;
}
|