• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Konsole

  • kde-4.14
  • applications
  • konsole
  • src
ColorSchemeEditor.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
3 
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.
8 
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.
13 
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.
18 */
19 
20 // Own
21 #include "ColorSchemeEditor.h"
22 
23 // Qt
24 #include <QtGui/QFontMetrics>
25 #include <QtCore/QFileInfo>
26 
27 // KDE
28 #include <KColorDialog>
29 #include <KWindowSystem>
30 #include <KFileDialog>
31 #include <KUrlCompletion>
32 
33 // Konsole
34 #include "ui_ColorSchemeEditor.h"
35 #include "ColorScheme.h"
36 #include "CharacterColor.h"
37 
38 using namespace Konsole;
39 
40 // colorTable is half the length of _table in ColorScheme class
41 // since intense colors are in a separated column
42 const int COLOR_TABLE_ROW_LENGTH = TABLE_COLORS / 2;
43 
44 const int NAME_COLUMN = 0; // column 0 : color names
45 const int COLOR_COLUMN = 1; // column 1 : actual colors
46 const int INTENSE_COLOR_COLUMN = 2; // column 2 : intense colors
47 
48 ColorSchemeEditor::ColorSchemeEditor(QWidget* aParent)
49  : KDialog(aParent)
50  , _isNewScheme(false)
51  , _colors(0)
52 {
53  // Kdialog buttons
54  setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
55  connect(this, SIGNAL(applyClicked()), this, SLOT(saveColorScheme()));
56  connect(this, SIGNAL(okClicked()), this, SLOT(saveColorScheme()));
57 
58  // ui
59  _ui = new Ui::ColorSchemeEditor();
60  _ui->setupUi(mainWidget());
61 
62  // description edit
63  _ui->descriptionEdit->setClearButtonShown(true);
64  connect(_ui->descriptionEdit , SIGNAL(textChanged(QString)) , this ,
65  SLOT(setDescription(QString)));
66 
67  // transparency slider
68  QFontMetrics metrics(font());
69  _ui->transparencyPercentLabel->setMinimumWidth(metrics.width("100%"));
70 
71  connect(_ui->transparencySlider , SIGNAL(valueChanged(int)) , this , SLOT(setTransparencyPercentLabel(int)));
72 
73  // randomized background
74  connect(_ui->randomizedBackgroundCheck , SIGNAL(toggled(bool)) , this ,
75  SLOT(setRandomizedBackgroundColor(bool)));
76 
77  // wallpaper stuff
78  KUrlCompletion* fileCompletion = new KUrlCompletion(KUrlCompletion::FileCompletion);
79  fileCompletion->setParent(this);
80  _ui->wallpaperPath->setCompletionObject(fileCompletion);
81  _ui->wallpaperPath->setClearButtonShown(true);
82  _ui->wallpaperSelectButton->setIcon(KIcon("image-x-generic"));
83 
84  connect(_ui->wallpaperSelectButton, SIGNAL(clicked()),
85  this, SLOT(selectWallpaper()));
86  connect(_ui->wallpaperPath, SIGNAL(textChanged(QString)),
87  this, SLOT(wallpaperPathChanged(QString)));
88 
89  // color table
90  _ui->colorTable->setColumnCount(3);
91  _ui->colorTable->setRowCount(COLOR_TABLE_ROW_LENGTH);
92 
93  QStringList labels;
94  labels << i18nc("@label:listbox Column header text for color names", "Name")
95  << i18nc("@label:listbox Column header text for the actual colors", "Color")
96  << i18nc("@label:listbox Column header text for the actual intense colors", "Intense color");
97  _ui->colorTable->setHorizontalHeaderLabels(labels);
98 
99  // Set resize mode for colorTable columns
100  _ui->colorTable->horizontalHeader()->setResizeMode(NAME_COLUMN, QHeaderView::ResizeToContents);
101  _ui->colorTable->horizontalHeader()->setResizeMode(COLOR_COLUMN, QHeaderView::Stretch);
102  _ui->colorTable->horizontalHeader()->setResizeMode(INTENSE_COLOR_COLUMN, QHeaderView::Stretch);
103 
104  QTableWidgetItem* item = new QTableWidgetItem("Test");
105  _ui->colorTable->setItem(0, 0, item);
106 
107  _ui->colorTable->verticalHeader()->hide();
108 
109  connect(_ui->colorTable , SIGNAL(itemClicked(QTableWidgetItem*)) , this ,
110  SLOT(editColorItem(QTableWidgetItem*)));
111 
112  // warning label when transparency is not available
113  _ui->transparencyWarningWidget->setWordWrap(true);
114  _ui->transparencyWarningWidget->setCloseButtonVisible(false);
115  _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);
116 
117  if (KWindowSystem::compositingActive()) {
118  _ui->transparencyWarningWidget->setVisible(false);
119  } else {
120  _ui->transparencyWarningWidget->setText(i18nc("@info:status",
121  "The background transparency setting will not"
122  " be used because your desktop does not appear to support"
123  " transparent windows."));
124  }
125 }
126 ColorSchemeEditor::~ColorSchemeEditor()
127 {
128  delete _colors;
129  delete _ui;
130 }
131 void ColorSchemeEditor::editColorItem(QTableWidgetItem* item)
132 {
133  // ignore if this is not a color column
134  if (item->column() != COLOR_COLUMN && item->column() != INTENSE_COLOR_COLUMN) {
135  return;
136  }
137 
138  QColor color = item->background().color();
139  int result = KColorDialog::getColor(color);
140 
141  if (result == KColorDialog::Accepted) {
142  item->setBackground(color);
143 
144  int colorSchemeRow = item->row();
145  // Intense colors row are in the bottom half of the color table
146  if (item->column() == INTENSE_COLOR_COLUMN) {
147  colorSchemeRow += COLOR_TABLE_ROW_LENGTH;
148  }
149 
150  ColorEntry entry(_colors->colorEntry(colorSchemeRow));
151  entry.color = color;
152  _colors->setColorTableEntry(colorSchemeRow, entry);
153 
154  emit colorsChanged(_colors);
155  }
156 }
157 void ColorSchemeEditor::selectWallpaper()
158 {
159  const KUrl url = KFileDialog::getImageOpenUrl(_ui->wallpaperPath->text(),
160  this,
161  i18nc("@action:button", "Select wallpaper image file"));
162 
163  if (!url.isEmpty())
164  _ui->wallpaperPath->setText(url.path());
165 }
166 void ColorSchemeEditor::wallpaperPathChanged(const QString& path)
167 {
168  if (path.isEmpty()) {
169  _colors->setWallpaper(path);
170  } else {
171  QFileInfo i(path);
172 
173  if (i.exists() && i.isFile() && i.isReadable())
174  _colors->setWallpaper(path);
175  }
176 }
177 void ColorSchemeEditor::setDescription(const QString& text)
178 {
179  if (_colors)
180  _colors->setDescription(text);
181 
182  if (_ui->descriptionEdit->text() != text)
183  _ui->descriptionEdit->setText(text);
184 }
185 void ColorSchemeEditor::setTransparencyPercentLabel(int percent)
186 {
187  _ui->transparencyPercentLabel->setText(QString("%1%").arg(percent));
188 
189  const qreal opacity = (100.0 - percent) / 100.0;
190  _colors->setOpacity(opacity);
191 }
192 void ColorSchemeEditor::setRandomizedBackgroundColor(bool randomize)
193 {
194  _colors->setRandomizedBackgroundColor(randomize);
195 }
196 void ColorSchemeEditor::setup(const ColorScheme* scheme, bool isNewScheme)
197 {
198  _isNewScheme = isNewScheme;
199 
200  delete _colors;
201 
202  _colors = new ColorScheme(*scheme);
203 
204  if (_isNewScheme) {
205  setCaption(i18n("New Color Scheme"));
206  setDescription(i18n("New Color Scheme"));
207  } else {
208  setCaption(i18n("Edit Color Scheme"));
209  }
210 
211  // setup description edit
212  _ui->descriptionEdit->setText(_colors->description());
213 
214  // setup color table
215  setupColorTable(_colors);
216 
217  // setup transparency slider
218  const int transparencyPercent = qRound((1 - _colors->opacity()) * 100);
219  _ui->transparencySlider->setValue(transparencyPercent);
220  setTransparencyPercentLabel(transparencyPercent);
221 
222  // randomized background color checkbox
223  _ui->randomizedBackgroundCheck->setChecked(scheme->randomizedBackgroundColor());
224 
225  // wallpaper stuff
226  _ui->wallpaperPath->setText(scheme->wallpaper()->path());
227 }
228 void ColorSchemeEditor::setupColorTable(const ColorScheme* colors)
229 {
230  ColorEntry table[TABLE_COLORS];
231  colors->getColorTable(table);
232 
233  for (int row = 0; row < COLOR_TABLE_ROW_LENGTH; row++) {
234  QTableWidgetItem* nameItem = new QTableWidgetItem(ColorScheme::translatedColorNameForIndex(row));
235  nameItem->setFlags(nameItem->flags() & ~Qt::ItemIsEditable);
236 
237  QTableWidgetItem* colorItem = new QTableWidgetItem();
238  colorItem->setBackground(table[row].color);
239  colorItem->setFlags(colorItem->flags() & ~Qt::ItemIsEditable & ~Qt::ItemIsSelectable);
240  colorItem->setToolTip(i18nc("@info:tooltip", "Click to choose color"));
241 
242  QTableWidgetItem* colorItemIntense = new QTableWidgetItem();
243  colorItemIntense->setBackground(table[COLOR_TABLE_ROW_LENGTH + row].color);
244  colorItemIntense->setFlags(colorItem->flags() & ~Qt::ItemIsEditable & ~Qt::ItemIsSelectable);
245  colorItemIntense->setToolTip(i18nc("@info:tooltip", "Click to choose intense color"));
246 
247  _ui->colorTable->setItem(row, NAME_COLUMN, nameItem);
248  _ui->colorTable->setItem(row, COLOR_COLUMN, colorItem);
249  _ui->colorTable->setItem(row, INTENSE_COLOR_COLUMN, colorItemIntense);
250  }
251  // ensure that color names are as fully visible as possible
252  _ui->colorTable->resizeColumnToContents(0);
253 
254  // set the widget height to the table content
255  _ui->colorTable->setFixedHeight(_ui->colorTable->verticalHeader()->length() + _ui->colorTable->horizontalHeader()->height() + 2);
256 }
257 
258 ColorScheme& ColorSchemeEditor::colorScheme() const
259 {
260  return *_colors;
261 }
262 bool ColorSchemeEditor::isNewScheme() const
263 {
264  return _isNewScheme;
265 }
266 void ColorSchemeEditor::saveColorScheme()
267 {
268  emit colorSchemeSaveRequested(colorScheme(), _isNewScheme);
269 }
270 
271 #include "ColorSchemeEditor.moc"
Konsole::ColorScheme::translatedColorNameForIndex
static QString translatedColorNameForIndex(int index)
Definition: ColorScheme.cpp:120
TABLE_COLORS
#define TABLE_COLORS
Definition: CharacterColor.h:117
QWidget
Konsole::ColorSchemeEditor::~ColorSchemeEditor
virtual ~ColorSchemeEditor()
Definition: ColorSchemeEditor.cpp:126
QTableWidgetItem
QTableWidgetItem::setBackground
void setBackground(const QBrush &brush)
CharacterColor.h
Konsole::ColorScheme::getColorTable
void getColorTable(ColorEntry *table, uint randomSeed=0) const
Copies the color entries which form the palette for this color scheme into table. ...
Definition: ColorScheme.cpp:222
Konsole::ColorScheme::setRandomizedBackgroundColor
void setRandomizedBackgroundColor(bool randomize)
Enables randomization of the background color.
Definition: ColorScheme.cpp:231
KDialog
QFontMetrics
NAME_COLUMN
const int NAME_COLUMN
Definition: ColorSchemeEditor.cpp:44
Konsole::ColorScheme::setOpacity
void setOpacity(qreal opacity)
Sets the opacity level of the display background.
Definition: ColorScheme.cpp:280
Konsole::ColorScheme::colorEntry
ColorEntry colorEntry(int index, uint randomSeed=0) const
Retrieves a single color entry from the table.
Definition: ColorScheme.cpp:193
QBrush::color
const QColor & color() const
Konsole::ColorScheme::opacity
qreal opacity() const
Returns the opacity level for this color scheme, see setOpacity() TODO: More documentation.
Definition: ColorScheme.cpp:284
ColorSchemeEditor.h
Konsole::ColorScheme::randomizedBackgroundColor
bool randomizedBackgroundColor() const
Returns true if the background color is randomized.
Definition: ColorScheme.cpp:227
Konsole::ColorEntry
An entry in a terminal display's color palette.
Definition: CharacterColor.h:40
QTableWidgetItem::column
int column() const
Konsole::ColorScheme::setColorTableEntry
void setColorTableEntry(int index, const ColorEntry &entry)
Sets a single entry within the color palette.
Definition: ColorScheme.cpp:180
QString::isEmpty
bool isEmpty() const
QString
QColor
Konsole::ColorSchemeEditor::setup
void setup(const ColorScheme *scheme, bool isNewScheme)
Initializes the dialog with the properties of the specified color scheme.
Definition: ColorSchemeEditor.cpp:196
QTableWidgetItem::row
int row() const
Konsole::ColorSchemeEditor::colorScheme
ColorScheme & colorScheme() const
Returns the modified color scheme.
Definition: ColorSchemeEditor.cpp:258
QStringList
Konsole::ColorScheme::description
QString description() const
Returns the descriptive name of the color scheme.
Definition: ColorScheme.cpp:166
Konsole::ColorScheme::setDescription
void setDescription(const QString &description)
Sets the descriptive name of the color scheme.
Definition: ColorScheme.cpp:162
Konsole::ColorSchemeEditor::colorSchemeSaveRequested
void colorSchemeSaveRequested(const ColorScheme &scheme, bool isNewScheme)
Used to send back colorscheme changes into the profile edited.
Konsole::ColorSchemeEditor::ColorSchemeEditor
ColorSchemeEditor(QWidget *parent=0)
Constructs a new color scheme editor with the specified parent.
Definition: ColorSchemeEditor.cpp:48
QFileInfo
QFontMetrics::width
int width(const QString &text, int len) const
ColorScheme.h
QTableWidgetItem::setFlags
void setFlags(QFlags< Qt::ItemFlag > flags)
Konsole::ColorSchemeEditor::colorsChanged
void colorsChanged(ColorScheme *scheme)
Emitted when the colors in the color scheme change.
INTENSE_COLOR_COLUMN
const int INTENSE_COLOR_COLUMN
Definition: ColorSchemeEditor.cpp:46
Konsole::ColorScheme::setWallpaper
void setWallpaper(const QString &path)
Definition: ColorScheme.cpp:365
QTableWidgetItem::background
QBrush background() const
Konsole::ColorEntry::color
QColor color
The color value of this entry for display.
Definition: CharacterColor.h:80
QTableWidgetItem::flags
Qt::ItemFlags flags() const
COLOR_TABLE_ROW_LENGTH
const int COLOR_TABLE_ROW_LENGTH
Definition: ColorSchemeEditor.cpp:42
Konsole::ColorSchemeEditor::isNewScheme
bool isNewScheme() const
Definition: ColorSchemeEditor.cpp:262
COLOR_COLUMN
const int COLOR_COLUMN
Definition: ColorSchemeEditor.cpp:45
QTableWidgetItem::setToolTip
void setToolTip(const QString &toolTip)
Konsole::ColorScheme
Represents a color scheme for a terminal display.
Definition: ColorScheme.h:72
Konsole::ColorSchemeEditor::setDescription
void setDescription(const QString &description)
Sets the text displayed in the description edit field.
Definition: ColorSchemeEditor.cpp:177
Konsole::ColorScheme::wallpaper
ColorSchemeWallpaper::Ptr wallpaper() const
Definition: ColorScheme.cpp:370
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal