1
/***************************************************************************
2
* fqterm, a terminal emulator for both BBS and *nix. *
3
* Copyright (C) 2008 fqterm development group. *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. *
19
***************************************************************************/
21
#include <QFileDialog>
22
#include <QColorDialog>
24
#include <QMessageBox>
26
#include "fqterm_config.h"
27
#include "fqterm_path.h"
28
#include "fqterm_trace.h"
30
#include "schemadialog.h"
34
schemaDialog::schemaDialog(QWidget *parent, Qt::WFlags fl)
35
: QDialog(parent, fl),
38
buttonGroup_.addButton(ui_.noneRadioButton, 0);
39
buttonGroup_.addButton(ui_.imageRadioButton, 1);
40
buttonGroup_.addButton(ui_.transpRadioButton, 2);
45
ui_.alphaSlider->setMinimum(0);
46
ui_.alphaSlider->setMaximum(100);
47
ui_.alphaSlider->setSingleStep(1);
48
ui_.alphaSlider->setPageStep(10);
50
ui_.imagePixmapLabel->setScaledContents(false);
51
// ui.bgButtonGroup->setRadioButtonExclusive(true);
52
colorButtons[0] = ui_.clr0Button;
53
colorButtons[1] = ui_.clr1Button;
54
colorButtons[2] = ui_.clr2Button;
55
colorButtons[3] = ui_.clr3Button;
56
colorButtons[4] = ui_.clr4Button;
57
colorButtons[5] = ui_.clr5Button;
58
colorButtons[6] = ui_.clr6Button;
59
colorButtons[7] = ui_.clr7Button;
60
colorButtons[8] = ui_.clr8Button;
61
colorButtons[9] = ui_.clr9Button;
62
colorButtons[10] = ui_.clr10Button;
63
colorButtons[11] = ui_.clr11Button;
64
colorButtons[12] = ui_.clr12Button;
65
colorButtons[13] = ui_.clr13Button;
66
colorButtons[14] = ui_.clr14Button;
67
colorButtons[15] = ui_.clr15Button;
74
schemaDialog::~schemaDialog(){}
76
void schemaDialog::setSchema(const QString &strSchemaFile) {
77
if (!QFile::exists(strSchemaFile)) {
81
int n = fileList_.indexOf(strSchemaFile);
82
ui_.nameListWidget->setCurrentRow(n);
85
QString schemaDialog::getSchema() {
86
return currentSchemaFileName_;
89
void schemaDialog::connectSlots() {
90
FQ_VERIFY(connect(ui_.saveButton, SIGNAL(clicked()), this, SLOT(saveSchema())));
91
FQ_VERIFY(connect(ui_.removeButton, SIGNAL(clicked()), this, SLOT(removeSchema())));
92
FQ_VERIFY(connect(ui_.okButton, SIGNAL(clicked()), this, SLOT(onOK())));
93
FQ_VERIFY(connect(ui_.cancelButton, SIGNAL(clicked()), this, SLOT(onCancel())));
95
for (int i = 0; i < 16; ++i) {
96
FQ_VERIFY(connect(colorButtons[i], SIGNAL(clicked()), this, SLOT(buttonClicked())));
99
FQ_VERIFY(connect(ui_.nameListWidget, SIGNAL(currentRowChanged(int)),
100
this, SLOT(nameChanged(int))));
102
// FQ_VERIFY(connect(&buttonGroup_, SIGNAL(clicked(int)), this, SLOT(bgType(int))));
103
FQ_VERIFY(connect(ui_.typeComboBox, SIGNAL(activated(int)), this, SLOT(imageType(int))));
104
FQ_VERIFY(connect(ui_.chooseButton, SIGNAL(clicked()), this, SLOT(chooseImage())));
105
FQ_VERIFY(connect(ui_.fadeButton, SIGNAL(clicked()), this, SLOT(fadeClicked())));
106
FQ_VERIFY(connect(ui_.alphaSlider, SIGNAL(valueChanged(int)), this, SLOT(alphaChanged(int))));
108
FQ_VERIFY(connect(ui_.titleLineEdit, SIGNAL(textChanged(const QString &)),
109
this, SLOT(textChanged(const QString &))));
110
FQ_VERIFY(connect(ui_.imageLineEdit, SIGNAL(textChanged(const QString &)),
111
this, SLOT(textChanged(const QString &))));
114
void schemaDialog::loadList() {
116
QFileInfoList lstFile;
119
dir.setNameFilters(QStringList("*.schema"));
120
dir.setPath(getPath(RESOURCE) + "schema");
121
lstFile = dir.entryInfoList();
123
//if(lstFile != NULL)
125
foreach(QFileInfo fi, lstFile) {
126
FQTermConfig *pConf = new FQTermConfig(fi.absoluteFilePath());
127
ui_.nameListWidget->addItem(pConf->getItemValue("schema", "title"));
129
fileList_.append(fi.absoluteFilePath());
132
if (ui_.nameListWidget->count() != 0) {
133
ui_.nameListWidget->setCurrentRow(0);
137
void schemaDialog::loadSchema(const QString &strSchemaFile) {
138
FQTermConfig *pConf = new FQTermConfig(strSchemaFile);
140
currentSchemaFileName_ = strSchemaFile;
142
title_ = pConf->getItemValue("schema", "title");
143
backgroundFileName_ = pConf->getItemValue("image", "name");
144
QString strTmp = pConf->getItemValue("image", "type");
145
backgroundType_ = strTmp.toInt();
146
fade_.setNamedColor(pConf->getItemValue("image", "fade"));
147
strTmp = pConf->getItemValue("image", "alpha");
148
alpha_ = strTmp.toFloat();
150
for (int i = 0; i < 16; ++i) {
151
colors[i].setNamedColor(pConf->getItemValue("color", QString("color%1").arg(i)));
160
void schemaDialog::saveNumSchema(int n) {
161
// FIXME: ?, and there is more below
162
QStringList::Iterator it = fileList_.begin();
167
title_ = ui_.titleLineEdit->text();
168
backgroundFileName_ = ui_.imageLineEdit->text();
170
QString schemaFileName = getPath(RESOURCE) + "schema/" + title_ + ".schema";
172
// create a new schema if title changed
173
if (schemaFileName != currentSchemaFileName_) {
174
ui_.nameListWidget->addItem(title_);
175
fileList_.append(schemaFileName);
178
FQTermConfig *pConf = new FQTermConfig(currentSchemaFileName_);
180
currentSchemaFileName_ = schemaFileName;
183
pConf->setItemValue("schema", "title", title_);
185
pConf->setItemValue("image", "name", backgroundFileName_);
188
strTmp.setNum(backgroundType_);
189
pConf->setItemValue("image", "type", strTmp);
191
pConf->setItemValue("image", "fade", fade_.name());
193
strTmp.setNum(alpha_);
194
pConf->setItemValue("image", "alpha", strTmp);
195
for (int i = 0; i < 16; ++i) {
196
pConf->setItemValue("color", QString("color%1").arg(i), colors[i].name());
199
pConf->save(schemaFileName);
207
void schemaDialog::updateView() {
209
ui_.titleLineEdit->setText(title_);
210
for (int i = 0; i < 16; ++i) {
212
palette.setColor(QPalette::Button, colors[i]);
213
colorButtons[i]->setPalette(palette);
217
switch (backgroundType_) {
220
buttonGroup_.button(0)->setChecked(true);
225
buttonGroup_.button(2)->setChecked(true);
230
buttonGroup_.button(1)->setChecked(true);
235
buttonGroup_.button(1)->setChecked(true);
240
buttonGroup_.button(1)->setChecked(true);
244
buttonGroup_.button(0)->setChecked(true);
250
ui_.imageLineEdit->setText(backgroundFileName_);
253
palette.setColor(ui_.fadeButton->backgroundRole(), fade_);
254
ui_.fadeButton->setPalette(palette);
257
ui_.alphaSlider->setValue(int(alpha_ * 100));
259
// load from file, nothing changed
263
void schemaDialog::updateBgPreview() {
265
palette.setColor(ui_.imagePixmapLabel->backgroundRole(), colors[0]);
266
ui_.imagePixmapLabel->setPalette(palette);
268
// ui.imagePixmapLabel->setPaletteBackgroundColor(clr0);
269
ui_.imagePixmapLabel->clear();
270
if (!QFile::exists(backgroundFileName_) || backgroundType_ == 0) {
275
QImage img(backgroundFileName_);
276
img = fadeColor(img, alpha_, fade_);
277
pixmap = QPixmap::fromImage(img.scaled(ui_.imagePixmapLabel->width(),
278
ui_.imagePixmapLabel->height()));
283
if( !pixmap.isNull() )
285
imagePixmapLabel->setPixmap( pixmap );
289
if( !pixmap.isNull() )
295
( size().width() - pixmap.width() ) / 2,
296
( size().height() - pixmap.height() ) / 2,
298
pixmap.width(), pixmap.height() );
299
imagePixmapLabel->setPixmap(pxm);
303
if( !pixmap.isNull() )
305
float sx = (float)size().width() / pixmap.width();
306
float sy = (float)size().height() /pixmap.height();
308
matrix.scale( sx, sy );
309
imagePixmapLabel->setPixmap(pixmap.xForm( matrix ));
314
ui_.imagePixmapLabel->setPixmap(pixmap);
318
void schemaDialog::buttonClicked() {
319
QPushButton *button = (QPushButton*)sender();
321
QColorDialog::getColor(button->palette().color(button->backgroundRole()));
322
if (color.isValid() == true) {
324
palette.setColor(QPalette::Button, color);
325
button->setPalette(palette);
328
for (int i = 0; i < 16; ++i) {
329
if (colorButtons[i] == button) {
336
void schemaDialog::nameChanged(int item) {
338
QMessageBox mb("FQTerm", "Setting changed, do you want to save?",
339
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::Default,
340
QMessageBox::No | QMessageBox::Escape, 0, this);
341
if (mb.exec() == QMessageBox::Yes) {
342
if (lastItemID_ != -1) {
343
saveNumSchema(lastItemID_);
348
QStringList::Iterator it = fileList_.begin();
349
int n = item; //nameListBox->index(item);
362
void schemaDialog::bgType(int n) {
366
// typeComboBox->setEnabled(true);
367
// imageLineEdit->setEnabled(true);
368
// chooseButton->setEnabled(true);
369
if (backgroundType_ == 0) {
372
ui_.typeComboBox->setCurrentIndex(backgroundType_ - 2);
377
// typeComboBox->setEnabled(false);
378
// imageLineEdit->setEnabled(false);
379
// chooseButton->setEnabled(false);
384
QMessageBox::information(this, "sorry",
385
"We are trying to bring you this function soon :)");
386
// typeComboBox->setEnabled(false);
387
// imageLineEdit->setEnabled(false);
388
// chooseButton->setEnabled(false);
389
// backgroundType_ = 1;
390
// bgButtonGroup->setButton(2);
391
buttonGroup_.button(2)->setChecked(true);
398
void schemaDialog::imageType(int n) {
399
backgroundType_ = n + 2;
404
void schemaDialog::chooseImage() {
405
QString img = QFileDialog::getOpenFileName(
406
this, "Choose an image", QDir::currentPath());
408
ui_.imageLineEdit->setText(img);
409
backgroundFileName_ = img;
410
backgroundType_ = 2+ui_.typeComboBox->currentIndex();
416
void schemaDialog::fadeClicked() {
417
QColor color = QColorDialog::getColor(fade_);
418
if (color.isValid() == TRUE) {
422
palette.setColor(ui_.fadeButton->backgroundRole(), color);
423
ui_.fadeButton->setPalette(palette);
425
// ui.fadeButton->setPaletteBackgroundColor(color);
427
ui_.fadeButton->setPalette(color);
435
void schemaDialog::alphaChanged(int value) {
436
alpha_ = float(value) / 100;
441
void schemaDialog::saveSchema() {
442
// get current schema file name
443
int n = ui_.nameListWidget->currentRow();
447
void schemaDialog::removeSchema() {
448
QFileInfo fi(currentSchemaFileName_);
449
if (fi.isWritable()) {
450
QFile::remove(currentSchemaFileName_);
451
QStringList::Iterator it = fileList_.begin();
452
int n = ui_.nameListWidget->currentRow();
453
ui_.nameListWidget->takeItem(n);
460
QMessageBox::warning(this, "Error",
461
"This is a system schema. Permission Denied");
465
void schemaDialog::onOK() {
467
QMessageBox mb("FQTerm", "Setting changed, do you want to save?",
468
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::Default,
469
QMessageBox::No | QMessageBox::Escape, 0, this);
470
if (mb.exec() == QMessageBox::Yes) {
471
int n = ui_.nameListWidget->currentRow();
479
void schemaDialog::onCancel() {
481
QMessageBox mb("FQTerm", "Setting changed, do you want to save?",
482
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::Default,
483
QMessageBox::No | QMessageBox::Escape, 0, this);
484
if (mb.exec() == QMessageBox::Yes) {
485
int n = ui_.nameListWidget->currentRow();
493
void schemaDialog::textChanged(const QString &) {
497
// from KImageEffect::fade
498
QImage &schemaDialog::fadeColor(QImage &img, float val, const QColor &color) {
499
if (img.width() == 0 || img.height() == 0) {
503
// We don't handle bitmaps
504
if (img.depth() == 1) {
508
unsigned char tbl[256];
509
for (int i = 0; i < 256; i++) {
510
tbl[i] = (int)(val *i + 0.5);
513
int red = color.red();
514
int green = color.green();
515
int blue = color.blue();
518
int r, g, b, cr, cg, cb;
520
if (img.depth() <= 8) {
522
for (int i = 0; i < img.numColors(); i++) {
528
r = cr - tbl[cr - red];
530
r = cr + tbl[red - cr];
534
g = cg - tbl[cg - green];
536
g = cg + tbl[green - cg];
540
b = cb - tbl[cb - blue];
542
b = cb + tbl[blue - cb];
545
img.setColor(i, qRgba(r, g, b, qAlpha(col)));
550
for (int y = 0; y < img.height(); y++) {
551
QRgb *data = (QRgb*)img.scanLine(y);
552
for (int x = 0; x < img.width(); x++) {
558
r = cr - tbl[cr - Qt::red];
560
r = cr + tbl[Qt::red - cr];
563
if (cg > Qt::green) {
564
g = cg - tbl[cg - Qt::green];
566
g = cg + tbl[Qt::green - cg];
570
b = cb - tbl[cb - Qt::blue];
572
b = cb + tbl[Qt::blue - cb];
575
*data++ = qRgba(r, g, b, qAlpha(col));
583
} // namespace FQTerm
585
#include "schemadialog.moc"