Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konsole / src / ColorSchemeEditor.cpp
blob97900fc91c16af9dfa22bed2c9c1661d7ae3f2fb
1 /*
2 Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301 USA.
20 // Own
21 #include "ColorSchemeEditor.h"
23 // Qt
24 #include <QtGui/QBrush>
25 #include <QtGui/QFontMetrics>
26 #include <QtGui/QHeaderView>
27 #include <QtGui/QItemDelegate>
28 #include <QtGui/QItemEditorCreator>
30 // KDE
31 #include <KColorDialog>
32 #include <KDebug>
33 #include <KWindowSystem>
35 // Konsole
36 #include "ui_ColorSchemeEditor.h"
37 #include "CharacterColor.h"
39 using namespace Konsole;
41 #if 0
42 class ColorEditorCreator : public QItemEditorCreatorBase
44 virtual QWidget* createWidget(QWidget* parent) const
46 return new KColorButton(parent);
49 virtual QByteArray valuePropertyName() const
51 return QByteArray("color");
54 #endif
56 ColorSchemeEditor::ColorSchemeEditor(QWidget* parent)
57 : QWidget(parent)
58 , _colors(0)
60 _ui = new Ui::ColorSchemeEditor();
61 _ui->setupUi(this);
63 // description edit
64 connect( _ui->descriptionEdit , SIGNAL(textChanged(const QString&)) , this ,
65 SLOT(setDescription(const QString&)) );
67 // transparency slider
68 QFontMetrics metrics(font());
69 _ui->transparencyPercentLabel->setMinimumWidth( metrics.width("100%") );
71 connect( _ui->transparencySlider , SIGNAL(valueChanged(int)) , this , SLOT(setTransparencyPercentLabel(int)) );
73 // randomized background
74 connect( _ui->randomizedBackgroundCheck , SIGNAL(toggled(bool)) , this ,
75 SLOT(setRandomizedBackgroundColor(bool)) );
77 // color table
78 _ui->colorTable->setColumnCount(2);
79 _ui->colorTable->setRowCount(TABLE_COLORS);
81 QStringList labels;
82 labels << i18n("Name") << i18n("Color");
83 _ui->colorTable->setHorizontalHeaderLabels(labels);
85 _ui->colorTable->horizontalHeader()->setStretchLastSection(true);
87 QTableWidgetItem* item = new QTableWidgetItem("Test");
88 _ui->colorTable->setItem(0,0,item);
90 _ui->colorTable->verticalHeader()->hide();
92 connect( _ui->colorTable , SIGNAL(itemClicked(QTableWidgetItem*)) , this ,
93 SLOT(editColorItem(QTableWidgetItem*)) );
95 // warning label when transparency is not available
96 if ( KWindowSystem::compositingActive() )
98 _ui->transparencyWarningWidget->setVisible(false);
100 else
102 _ui->transparencyWarningWidget->setText(i18n("The background transparency setting will not"
103 " be used because your desktop does not appear to support"
104 " transparent windows."));
107 void ColorSchemeEditor::setRandomizedBackgroundColor( bool randomize )
109 _colors->setRandomizedBackgroundColor(randomize);
111 ColorSchemeEditor::~ColorSchemeEditor()
113 delete _colors;
114 delete _ui;
116 void ColorSchemeEditor::editColorItem( QTableWidgetItem* item )
118 // ignore if this is not a color column
119 if ( item->column() != 1 )
120 return;
122 KColorDialog* dialog = new KColorDialog(this);
123 dialog->setColor( item->background().color() );
125 dialog->exec();
127 item->setBackground( dialog->color() );
129 ColorEntry entry(_colors->colorEntry(item->row()));
130 entry.color = dialog->color();
131 _colors->setColorTableEntry(item->row(),entry);
133 emit colorsChanged(_colors);
135 void ColorSchemeEditor::setDescription(const QString& text)
137 if ( _colors )
138 _colors->setDescription(text);
140 if ( _ui->descriptionEdit->text() != text )
141 _ui->descriptionEdit->setText(text);
143 void ColorSchemeEditor::setTransparencyPercentLabel(int percent)
145 _ui->transparencyPercentLabel->setText( QString("%1%").arg(percent) );
147 qreal opacity = ( 100.0 - percent ) / 100.0;
148 _colors->setOpacity(opacity);
150 void ColorSchemeEditor::setup(const ColorScheme* scheme)
152 if ( _colors )
153 delete _colors;
155 _colors = new ColorScheme(*scheme);
157 // setup description edit
158 _ui->descriptionEdit->setText(_colors->description());
160 // setup color table
161 setupColorTable(_colors);
163 // setup transparency slider
164 const int transparencyPercent = qRound( (1-_colors->opacity())*100 );
166 _ui->transparencySlider->setValue(transparencyPercent);
167 setTransparencyPercentLabel(transparencyPercent);
169 // randomized background color checkbox
170 _ui->randomizedBackgroundCheck->setChecked( scheme->randomizedBackgroundColor() );
172 void ColorSchemeEditor::setupColorTable(const ColorScheme* colors)
174 ColorEntry table[TABLE_COLORS];
175 colors->getColorTable(table);
177 for ( int row = 0 ; row < TABLE_COLORS ; row++ )
179 QTableWidgetItem* nameItem = new QTableWidgetItem( ColorScheme::translatedColorNameForIndex(row) );
180 QTableWidgetItem* colorItem = new QTableWidgetItem();
181 colorItem->setBackground( table[row].color );
182 colorItem->setFlags( colorItem->flags() & ~Qt::ItemIsEditable & ~Qt::ItemIsSelectable );
184 _ui->colorTable->setItem(row,0,nameItem);
185 _ui->colorTable->setItem(row,1,colorItem);
188 // ensure that color names are as fully visible as possible
189 _ui->colorTable->resizeColumnToContents(0);
191 ColorScheme* ColorSchemeEditor::colorScheme() const
193 return _colors;
196 #include "ColorSchemeEditor.moc"