ColorSchemeEditor.cpp 9.51 KB
Newer Older
1
/*
2
    Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

20 21 22
// Own
#include "ColorSchemeEditor.h"

23
// Qt
Dirk Mueller's avatar
Dirk Mueller committed
24
#include <QtGui/QFontMetrics>
25
#include <QtCore/QFileInfo>
26 27 28

// KDE
#include <KColorDialog>
29
#include <KWindowSystem>
30 31
#include <KFileDialog>
#include <KUrlCompletion>
32 33 34

// Konsole
#include "ui_ColorSchemeEditor.h"
Jekyll Wu's avatar
Jekyll Wu committed
35
#include "ColorScheme.h"
36
#include "CharacterColor.h"
37 38 39

using namespace Konsole;

40 41 42 43 44 45 46 47
// colorTable is half the length of _table in ColorScheme class
// since intense colors are in a separated column
const int COLOR_TABLE_ROW_LENGTH =  TABLE_COLORS / 2; 

const int NAME_COLUMN = 0;           // column 0 : color names
const int COLOR_COLUMN = 1;          // column 1 : actual colors
const int INTENSE_COLOR_COLUMN = 2;  // column 2 : intense colors

Kurt Hindenburg's avatar
Kurt Hindenburg committed
48
ColorSchemeEditor::ColorSchemeEditor(QWidget* aParent)
49
    : KDialog(aParent)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
50
    , _isNewScheme(false)
51 52
    , _colors(0)
{
53 54 55 56 57 58
    // Kdialog buttons
    setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
    connect(this, SIGNAL(applyClicked()), this, SLOT(saveColorScheme()));
    connect(this, SIGNAL(okClicked()), this, SLOT(saveColorScheme()));

    // ui
59
    _ui = new Ui::ColorSchemeEditor();
60
    _ui->setupUi(mainWidget());
61 62

    // description edit
63
    _ui->descriptionEdit->setClearButtonShown(true);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
64 65
    connect(_ui->descriptionEdit , SIGNAL(textChanged(QString)) , this ,
            SLOT(setDescription(QString)));
66 67 68

    // transparency slider
    QFontMetrics metrics(font());
Kurt Hindenburg's avatar
Kurt Hindenburg committed
69
    _ui->transparencyPercentLabel->setMinimumWidth(metrics.width("100%"));
70

Kurt Hindenburg's avatar
Kurt Hindenburg committed
71
    connect(_ui->transparencySlider , SIGNAL(valueChanged(int)) , this , SLOT(setTransparencyPercentLabel(int)));
72

73
    // randomized background
Kurt Hindenburg's avatar
Kurt Hindenburg committed
74 75
    connect(_ui->randomizedBackgroundCheck , SIGNAL(toggled(bool)) , this ,
            SLOT(setRandomizedBackgroundColor(bool)));
76

Jekyll Wu's avatar
Jekyll Wu committed
77 78 79 80 81 82 83 84 85 86 87 88
    // wallpaper stuff
    KUrlCompletion* fileCompletion = new KUrlCompletion(KUrlCompletion::FileCompletion);
    fileCompletion->setParent(this);
    _ui->wallpaperPath->setCompletionObject(fileCompletion);
    _ui->wallpaperPath->setClearButtonShown(true);
    _ui->wallpaperSelectButton->setIcon(KIcon("image-x-generic"));

    connect(_ui->wallpaperSelectButton, SIGNAL(clicked()),
            this, SLOT(selectWallpaper()));
    connect(_ui->wallpaperPath, SIGNAL(textChanged(QString)),
            this, SLOT(wallpaperPathChanged(QString)));

89
    // color table
90 91
    _ui->colorTable->setColumnCount(3);
    _ui->colorTable->setRowCount(COLOR_TABLE_ROW_LENGTH);
92 93

    QStringList labels;
94 95 96
    labels << i18nc("@label:listbox Column header text for color names", "Name")
           << i18nc("@label:listbox Column header text for the actual colors", "Color")
           << i18nc("@label:listbox Column header text for the actual intense colors", "Intense color");
97 98
    _ui->colorTable->setHorizontalHeaderLabels(labels);

99 100 101 102
    // Set resize mode for colorTable columns
    _ui->colorTable->horizontalHeader()->setResizeMode(NAME_COLUMN, QHeaderView::ResizeToContents);
    _ui->colorTable->horizontalHeader()->setResizeMode(COLOR_COLUMN, QHeaderView::Stretch);
    _ui->colorTable->horizontalHeader()->setResizeMode(INTENSE_COLOR_COLUMN, QHeaderView::Stretch);
103 104

    QTableWidgetItem* item = new QTableWidgetItem("Test");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
105
    _ui->colorTable->setItem(0, 0, item);
106 107 108

    _ui->colorTable->verticalHeader()->hide();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
109 110
    connect(_ui->colorTable , SIGNAL(itemClicked(QTableWidgetItem*)) , this ,
            SLOT(editColorItem(QTableWidgetItem*)));
111

Jekyll Wu's avatar
Jekyll Wu committed
112
    // warning label when transparency is not available
113 114 115 116
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
117
    if (KWindowSystem::compositingActive()) {
118
        _ui->transparencyWarningWidget->setVisible(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
119
    } else {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
120
        _ui->transparencyWarningWidget->setText(i18nc("@info:status",
Kurt Hindenburg's avatar
Kurt Hindenburg committed
121 122 123
                                                "The background transparency setting will not"
                                                " be used because your desktop does not appear to support"
                                                " transparent windows."));
124
    }
125
}
126 127 128 129 130
ColorSchemeEditor::~ColorSchemeEditor()
{
    delete _colors;
    delete _ui;
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
131
void ColorSchemeEditor::editColorItem(QTableWidgetItem* item)
132 133
{
    // ignore if this is not a color column
134 135 136
    if (item->column() != COLOR_COLUMN && item->column() != INTENSE_COLOR_COLUMN) {
        return;
    }
137

138
    QColor color = item->background().color();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
139
    int result = KColorDialog::getColor(color);
140

Kurt Hindenburg's avatar
Kurt Hindenburg committed
141 142
    if (result == KColorDialog::Accepted) {
        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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
154
        emit colorsChanged(_colors);
155
    }
156
}
157 158
void ColorSchemeEditor::selectWallpaper()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
159 160 161
    const KUrl url = KFileDialog::getImageOpenUrl(_ui->wallpaperPath->text(),
                     this,
                     i18nc("@action:button", "Select wallpaper image file"));
162

Kurt Hindenburg's avatar
Kurt Hindenburg committed
163
    if (!url.isEmpty())
164 165 166 167
        _ui->wallpaperPath->setText(url.path());
}
void ColorSchemeEditor::wallpaperPathChanged(const QString& path)
{
Jekyll Wu's avatar
Jekyll Wu committed
168
    if (path.isEmpty()) {
169
        _colors->setWallpaper(path);
170
    } else {
171 172 173 174 175 176
        QFileInfo i(path);

        if (i.exists() && i.isFile() && i.isReadable())
            _colors->setWallpaper(path);
    }
}
177 178
void ColorSchemeEditor::setDescription(const QString& text)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
179
    if (_colors)
180 181
        _colors->setDescription(text);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
182
    if (_ui->descriptionEdit->text() != text)
183 184 185 186
        _ui->descriptionEdit->setText(text);
}
void ColorSchemeEditor::setTransparencyPercentLabel(int percent)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
187
    _ui->transparencyPercentLabel->setText(QString("%1%").arg(percent));
188

Jekyll Wu's avatar
Jekyll Wu committed
189
    const qreal opacity = (100.0 - percent) / 100.0;
190 191
    _colors->setOpacity(opacity);
}
Jekyll Wu's avatar
Jekyll Wu committed
192 193 194 195
void ColorSchemeEditor::setRandomizedBackgroundColor(bool randomize)
{
    _colors->setRandomizedBackgroundColor(randomize);
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
196
void ColorSchemeEditor::setup(const ColorScheme* scheme, bool isNewScheme)
197
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
198 199
    _isNewScheme = isNewScheme;

200
    delete _colors;
201 202 203

    _colors = new ColorScheme(*scheme);

204 205 206 207 208 209 210
    if (_isNewScheme) {
        setCaption(i18n("New Color Scheme"));
        setDescription(i18n("New Color Scheme"));
    } else {
        setCaption(i18n("Edit Color Scheme"));
    }

211 212 213 214 215 216 217
    // setup description edit
    _ui->descriptionEdit->setText(_colors->description());

    // setup color table
    setupColorTable(_colors);

    // setup transparency slider
Kurt Hindenburg's avatar
Kurt Hindenburg committed
218
    const int transparencyPercent = qRound((1 - _colors->opacity()) * 100);
219 220
    _ui->transparencySlider->setValue(transparencyPercent);
    setTransparencyPercentLabel(transparencyPercent);
221 222

    // randomized background color checkbox
Kurt Hindenburg's avatar
Kurt Hindenburg committed
223
    _ui->randomizedBackgroundCheck->setChecked(scheme->randomizedBackgroundColor());
224 225

    // wallpaper stuff
Kurt Hindenburg's avatar
Kurt Hindenburg committed
226
    _ui->wallpaperPath->setText(scheme->wallpaper()->path());
227 228 229
}
void ColorSchemeEditor::setupColorTable(const ColorScheme* colors)
{
230 231
    ColorEntry table[TABLE_COLORS];
    colors->getColorTable(table);
232

233
    for (int row = 0; row < COLOR_TABLE_ROW_LENGTH; row++) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
234
        QTableWidgetItem* nameItem = new QTableWidgetItem(ColorScheme::translatedColorNameForIndex(row));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
235
        nameItem->setFlags(nameItem->flags() & ~Qt::ItemIsEditable);
236

237
        QTableWidgetItem* colorItem = new QTableWidgetItem();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
238 239
        colorItem->setBackground(table[row].color);
        colorItem->setFlags(colorItem->flags() & ~Qt::ItemIsEditable & ~Qt::ItemIsSelectable);
240
        colorItem->setToolTip(i18nc("@info:tooltip", "Click to choose color"));
241

242 243 244 245
        QTableWidgetItem* colorItemIntense = new QTableWidgetItem();
        colorItemIntense->setBackground(table[COLOR_TABLE_ROW_LENGTH + row].color);
        colorItemIntense->setFlags(colorItem->flags() & ~Qt::ItemIsEditable & ~Qt::ItemIsSelectable);
        colorItemIntense->setToolTip(i18nc("@info:tooltip", "Click to choose intense color"));
246

247 248 249 250
        _ui->colorTable->setItem(row, NAME_COLUMN, nameItem);
        _ui->colorTable->setItem(row, COLOR_COLUMN, colorItem);
        _ui->colorTable->setItem(row, INTENSE_COLOR_COLUMN, colorItemIntense);
    }
251 252
    // ensure that color names are as fully visible as possible
    _ui->colorTable->resizeColumnToContents(0);
253 254 255

    // set the widget height to the table content
    _ui->colorTable->setFixedHeight(_ui->colorTable->verticalHeader()->length() + _ui->colorTable->horizontalHeader()->height() + 2);
256
}
257

258
ColorScheme& ColorSchemeEditor::colorScheme() const
259
{
260
    return *_colors;
261
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
262 263 264 265
bool ColorSchemeEditor::isNewScheme() const
{
    return _isNewScheme;
}
266 267 268 269
void ColorSchemeEditor::saveColorScheme()
{
    emit colorSchemeSaveRequested(colorScheme(), _isNewScheme);
}
270 271

#include "ColorSchemeEditor.moc"