ColorSchemeEditor.cpp 9.69 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
#include <QCompleter>
#include <QFileSystemModel>
28 29 30

// KDE
#include <KColorDialog>
31
#include <KWindowSystem>
32 33
#include <KFileDialog>
#include <KUrlCompletion>
Alex Richardson's avatar
Alex Richardson committed
34 35 36
#include <KIcon>
#include <KUrl>
#include <KLocalizedString>
37 38 39

// Konsole
#include "ui_ColorSchemeEditor.h"
Jekyll Wu's avatar
Jekyll Wu committed
40
#include "ColorScheme.h"
41
#include "CharacterColor.h"
42 43 44

using namespace Konsole;

45 46
// colorTable is half the length of _table in ColorScheme class
// since intense colors are in a separated column
47
const int COLOR_TABLE_ROW_LENGTH =  TABLE_COLORS / 2;
48 49 50 51 52

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
53
ColorSchemeEditor::ColorSchemeEditor(QWidget* aParent)
54
    : KDialog(aParent)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
55
    , _isNewScheme(false)
56 57
    , _colors(0)
{
58 59 60 61 62 63
    // Kdialog buttons
    setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
    connect(this, SIGNAL(applyClicked()), this, SLOT(saveColorScheme()));
    connect(this, SIGNAL(okClicked()), this, SLOT(saveColorScheme()));

    // ui
64
    _ui = new Ui::ColorSchemeEditor();
65
    _ui->setupUi(mainWidget());
66 67

    // description edit
Kurt Hindenburg's avatar
Kurt Hindenburg committed
68
    _ui->descriptionEdit->setClearButtonEnabled(true);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
69 70
    connect(_ui->descriptionEdit , SIGNAL(textChanged(QString)) , this ,
            SLOT(setDescription(QString)));
71 72 73

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

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

78
    // randomized background
Kurt Hindenburg's avatar
Kurt Hindenburg committed
79 80
    connect(_ui->randomizedBackgroundCheck , SIGNAL(toggled(bool)) , this ,
            SLOT(setRandomizedBackgroundColor(bool)));
81

Jekyll Wu's avatar
Jekyll Wu committed
82
    // wallpaper stuff
83 84 85 86 87 88 89
    QFileSystemModel *dirModel = new QFileSystemModel(this);
    dirModel->setFilter(QDir::AllEntries);
    dirModel->setRootPath(QString('/'));
    QCompleter *completer = new QCompleter(this);
    completer->setModel(dirModel); 
    _ui->wallpaperPath->setCompleter(completer);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
90
    _ui->wallpaperPath->setClearButtonEnabled(true);
Jekyll Wu's avatar
Jekyll Wu committed
91 92 93 94 95 96 97
    _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)));

98
    // color table
99 100
    _ui->colorTable->setColumnCount(3);
    _ui->colorTable->setRowCount(COLOR_TABLE_ROW_LENGTH);
101 102

    QStringList labels;
103 104 105
    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");
106 107
    _ui->colorTable->setHorizontalHeaderLabels(labels);

108 109 110 111
    // 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);
112 113

    QTableWidgetItem* item = new QTableWidgetItem("Test");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
114
    _ui->colorTable->setItem(0, 0, item);
115 116 117

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
118 119
    connect(_ui->colorTable , SIGNAL(itemClicked(QTableWidgetItem*)) , this ,
            SLOT(editColorItem(QTableWidgetItem*)));
120

Jekyll Wu's avatar
Jekyll Wu committed
121
    // warning label when transparency is not available
122 123 124 125
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);

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

147
    QColor color = item->background().color();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
148
    int result = KColorDialog::getColor(color);
149

Kurt Hindenburg's avatar
Kurt Hindenburg committed
150 151
    if (result == KColorDialog::Accepted) {
        item->setBackground(color);
152

153
        int colorSchemeRow = item->row();
154
        // Intense colors row are in the bottom half of the color table
155
        if (item->column() == INTENSE_COLOR_COLUMN) {
156
            colorSchemeRow += COLOR_TABLE_ROW_LENGTH;
157
        }
158 159

        ColorEntry entry(_colors->colorEntry(colorSchemeRow));
160
        entry.color = color;
161
        _colors->setColorTableEntry(colorSchemeRow, entry);
162

Kurt Hindenburg's avatar
Kurt Hindenburg committed
163
        emit colorsChanged(_colors);
164
    }
165
}
166 167
void ColorSchemeEditor::selectWallpaper()
{
Alex Richardson's avatar
Alex Richardson committed
168
    const KUrl url = KFileDialog::getImageOpenUrl(KUrl(_ui->wallpaperPath->text()),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
169 170
                     this,
                     i18nc("@action:button", "Select wallpaper image file"));
171

Kurt Hindenburg's avatar
Kurt Hindenburg committed
172
    if (!url.isEmpty())
173 174 175 176
        _ui->wallpaperPath->setText(url.path());
}
void ColorSchemeEditor::wallpaperPathChanged(const QString& path)
{
Jekyll Wu's avatar
Jekyll Wu committed
177
    if (path.isEmpty()) {
178
        _colors->setWallpaper(path);
179
    } else {
180 181 182 183 184 185
        QFileInfo i(path);

        if (i.exists() && i.isFile() && i.isReadable())
            _colors->setWallpaper(path);
    }
}
186 187
void ColorSchemeEditor::setDescription(const QString& text)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
188
    if (_colors)
189 190
        _colors->setDescription(text);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
191
    if (_ui->descriptionEdit->text() != text)
192 193 194 195
        _ui->descriptionEdit->setText(text);
}
void ColorSchemeEditor::setTransparencyPercentLabel(int percent)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
196
    _ui->transparencyPercentLabel->setText(QString("%1%").arg(percent));
197

Jekyll Wu's avatar
Jekyll Wu committed
198
    const qreal opacity = (100.0 - percent) / 100.0;
199 200
    _colors->setOpacity(opacity);
}
Jekyll Wu's avatar
Jekyll Wu committed
201 202 203 204
void ColorSchemeEditor::setRandomizedBackgroundColor(bool randomize)
{
    _colors->setRandomizedBackgroundColor(randomize);
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
205
void ColorSchemeEditor::setup(const ColorScheme* scheme, bool isNewScheme)
206
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
207 208
    _isNewScheme = isNewScheme;

209
    delete _colors;
210 211 212

    _colors = new ColorScheme(*scheme);

213 214 215 216 217 218 219
    if (_isNewScheme) {
        setCaption(i18n("New Color Scheme"));
        setDescription(i18n("New Color Scheme"));
    } else {
        setCaption(i18n("Edit Color Scheme"));
    }

220 221 222 223 224 225 226
    // setup description edit
    _ui->descriptionEdit->setText(_colors->description());

    // setup color table
    setupColorTable(_colors);

    // setup transparency slider
Kurt Hindenburg's avatar
Kurt Hindenburg committed
227
    const int transparencyPercent = qRound((1 - _colors->opacity()) * 100);
228 229
    _ui->transparencySlider->setValue(transparencyPercent);
    setTransparencyPercentLabel(transparencyPercent);
230 231

    // randomized background color checkbox
Kurt Hindenburg's avatar
Kurt Hindenburg committed
232
    _ui->randomizedBackgroundCheck->setChecked(scheme->randomizedBackgroundColor());
233 234

    // wallpaper stuff
Kurt Hindenburg's avatar
Kurt Hindenburg committed
235
    _ui->wallpaperPath->setText(scheme->wallpaper()->path());
236 237 238
}
void ColorSchemeEditor::setupColorTable(const ColorScheme* colors)
{
239 240
    ColorEntry table[TABLE_COLORS];
    colors->getColorTable(table);
241

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

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

251 252 253 254
        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"));
255

256 257 258 259
        _ui->colorTable->setItem(row, NAME_COLUMN, nameItem);
        _ui->colorTable->setItem(row, COLOR_COLUMN, colorItem);
        _ui->colorTable->setItem(row, INTENSE_COLOR_COLUMN, colorItemIntense);
    }
260 261
    // ensure that color names are as fully visible as possible
    _ui->colorTable->resizeColumnToContents(0);
262 263 264

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

267
ColorScheme& ColorSchemeEditor::colorScheme() const
268
{
269
    return *_colors;
270
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
271 272 273 274
bool ColorSchemeEditor::isNewScheme() const
{
    return _isNewScheme;
}
275 276 277 278
void ColorSchemeEditor::saveColorScheme()
{
    emit colorSchemeSaveRequested(colorScheme(), _isNewScheme);
}
279