ColorSchemeEditor.cpp 7.21 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;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
40 41
ColorSchemeEditor::ColorSchemeEditor(QWidget* aParent)
    : QWidget(aParent)
42 43 44 45 46 47
    , _colors(0)
{
    _ui = new Ui::ColorSchemeEditor();
    _ui->setupUi(this);

    // description edit
48
    _ui->descriptionEdit->setClearButtonShown(true);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
49 50
    connect(_ui->descriptionEdit , SIGNAL(textChanged(QString)) , this ,
            SLOT(setDescription(QString)));
51 52 53

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

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

58
    // randomized background
Kurt Hindenburg's avatar
Kurt Hindenburg committed
59 60
    connect(_ui->randomizedBackgroundCheck , SIGNAL(toggled(bool)) , this ,
            SLOT(setRandomizedBackgroundColor(bool)));
61

Jekyll Wu's avatar
Jekyll Wu committed
62 63 64 65 66 67 68 69 70 71 72 73
    // 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)));

74 75 76 77 78
    // color table
    _ui->colorTable->setColumnCount(2);
    _ui->colorTable->setRowCount(TABLE_COLORS);

    QStringList labels;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
79
    labels << i18nc("@label:listbox Column header text for color names", "Name") << i18nc("@label:listbox Column header text for the actual colors", "Color");
80 81 82 83 84
    _ui->colorTable->setHorizontalHeaderLabels(labels);

    _ui->colorTable->horizontalHeader()->setStretchLastSection(true);

    QTableWidgetItem* item = new QTableWidgetItem("Test");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
85
    _ui->colorTable->setItem(0, 0, item);
86 87 88

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
89 90
    connect(_ui->colorTable , SIGNAL(itemClicked(QTableWidgetItem*)) , this ,
            SLOT(editColorItem(QTableWidgetItem*)));
91

Jekyll Wu's avatar
Jekyll Wu committed
92
    // warning label when transparency is not available
93 94 95 96
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
97
    if (KWindowSystem::compositingActive()) {
98
        _ui->transparencyWarningWidget->setVisible(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
99
    } else {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
100
        _ui->transparencyWarningWidget->setText(i18nc("@info:status",
Kurt Hindenburg's avatar
Kurt Hindenburg committed
101 102 103
                                                "The background transparency setting will not"
                                                " be used because your desktop does not appear to support"
                                                " transparent windows."));
104
    }
105
}
106 107 108 109 110
ColorSchemeEditor::~ColorSchemeEditor()
{
    delete _colors;
    delete _ui;
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
111
void ColorSchemeEditor::editColorItem(QTableWidgetItem* item)
112
{
Jekyll Wu's avatar
Jekyll Wu committed
113 114
    const int COLOR_COLUMN = 1;

115
    // ignore if this is not a color column
Jekyll Wu's avatar
Jekyll Wu committed
116
    if (item->column() != COLOR_COLUMN)
117 118
        return;

119
    QColor color = item->background().color();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
120 121 122
    int result = KColorDialog::getColor(color);
    if (result == KColorDialog::Accepted) {
        item->setBackground(color);
123

Kurt Hindenburg's avatar
Kurt Hindenburg committed
124
        ColorEntry entry(_colors->colorEntry(item->row()));
125
        entry.color = color;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
126
        _colors->setColorTableEntry(item->row(), entry);
127

Kurt Hindenburg's avatar
Kurt Hindenburg committed
128
        emit colorsChanged(_colors);
129

130
    }
131
}
132 133
void ColorSchemeEditor::selectWallpaper()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
134 135 136
    const KUrl url = KFileDialog::getImageOpenUrl(_ui->wallpaperPath->text(),
                     this,
                     i18nc("@action:button", "Select wallpaper image file"));
137

Kurt Hindenburg's avatar
Kurt Hindenburg committed
138
    if (!url.isEmpty())
139 140 141 142
        _ui->wallpaperPath->setText(url.path());
}
void ColorSchemeEditor::wallpaperPathChanged(const QString& path)
{
Jekyll Wu's avatar
Jekyll Wu committed
143
    if (path.isEmpty()) {
144
        _colors->setWallpaper(path);
145
    } else {
146 147 148 149 150 151
        QFileInfo i(path);

        if (i.exists() && i.isFile() && i.isReadable())
            _colors->setWallpaper(path);
    }
}
152 153
void ColorSchemeEditor::setDescription(const QString& text)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
154
    if (_colors)
155 156
        _colors->setDescription(text);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
157
    if (_ui->descriptionEdit->text() != text)
158 159 160 161
        _ui->descriptionEdit->setText(text);
}
void ColorSchemeEditor::setTransparencyPercentLabel(int percent)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
162
    _ui->transparencyPercentLabel->setText(QString("%1%").arg(percent));
163

Jekyll Wu's avatar
Jekyll Wu committed
164
    const qreal opacity = (100.0 - percent) / 100.0;
165 166
    _colors->setOpacity(opacity);
}
Jekyll Wu's avatar
Jekyll Wu committed
167 168 169 170
void ColorSchemeEditor::setRandomizedBackgroundColor(bool randomize)
{
    _colors->setRandomizedBackgroundColor(randomize);
}
171 172
void ColorSchemeEditor::setup(const ColorScheme* scheme)
{
173
    delete _colors;
174 175 176 177 178 179 180 181 182 183

    _colors = new ColorScheme(*scheme);

    // setup description edit
    _ui->descriptionEdit->setText(_colors->description());

    // setup color table
    setupColorTable(_colors);

    // setup transparency slider
Kurt Hindenburg's avatar
Kurt Hindenburg committed
184
    const int transparencyPercent = qRound((1 - _colors->opacity()) * 100);
185 186
    _ui->transparencySlider->setValue(transparencyPercent);
    setTransparencyPercentLabel(transparencyPercent);
187 188

    // randomized background color checkbox
Kurt Hindenburg's avatar
Kurt Hindenburg committed
189
    _ui->randomizedBackgroundCheck->setChecked(scheme->randomizedBackgroundColor());
190 191

    // wallpaper stuff
Kurt Hindenburg's avatar
Kurt Hindenburg committed
192
    _ui->wallpaperPath->setText(scheme->wallpaper()->path());
193 194 195
}
void ColorSchemeEditor::setupColorTable(const ColorScheme* colors)
{
196 197
    ColorEntry table[TABLE_COLORS];
    colors->getColorTable(table);
198

Kurt Hindenburg's avatar
Kurt Hindenburg committed
199 200
    for (int row = 0 ; row < TABLE_COLORS ; row++) {
        QTableWidgetItem* nameItem = new QTableWidgetItem(ColorScheme::translatedColorNameForIndex(row));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
201
        nameItem->setFlags(nameItem->flags() & ~Qt::ItemIsEditable);
202

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
208 209
        _ui->colorTable->setItem(row, 0, nameItem);
        _ui->colorTable->setItem(row, 1, colorItem);
210
    }
211 212 213

    // ensure that color names are as fully visible as possible
    _ui->colorTable->resizeColumnToContents(0);
214 215 216 217 218 219 220
}
ColorScheme* ColorSchemeEditor::colorScheme() const
{
    return _colors;
}

#include "ColorSchemeEditor.moc"