ColorSchemeEditor.cpp 7.15 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 <KDebug>
30
#include <KWindowSystem>
31 32
#include <KFileDialog>
#include <KUrlCompletion>
33 34 35

// Konsole
#include "ui_ColorSchemeEditor.h"
Jekyll Wu's avatar
Jekyll Wu committed
36
#include "ColorScheme.h"
37
#include "CharacterColor.h"
38 39 40 41 42 43 44 45 46 47 48

using namespace Konsole;

ColorSchemeEditor::ColorSchemeEditor(QWidget* parent)
    : QWidget(parent)
    , _colors(0)
{
    _ui = new Ui::ColorSchemeEditor();
    _ui->setupUi(this);

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

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

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

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

63 64 65 66 67
    // color table
    _ui->colorTable->setColumnCount(2);
    _ui->colorTable->setRowCount(TABLE_COLORS);

    QStringList labels;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
68
    labels << i18nc("@label:listbox Column header text for color names", "Name") << i18nc("@label:listbox Column header text for the actual colors", "Color");
69 70 71 72 73
    _ui->colorTable->setHorizontalHeaderLabels(labels);

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

    QTableWidgetItem* item = new QTableWidgetItem("Test");
Kurt Hindenburg's avatar
Kurt Hindenburg committed
74
    _ui->colorTable->setItem(0, 0, item);
75 76 77

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
78 79
    connect(_ui->colorTable , SIGNAL(itemClicked(QTableWidgetItem*)) , this ,
            SLOT(editColorItem(QTableWidgetItem*)));
80

81 82 83 84
    _ui->transparencyWarningWidget->setWordWrap(true);
    _ui->transparencyWarningWidget->setCloseButtonVisible(false);
    _ui->transparencyWarningWidget->setMessageType(KMessageWidget::Warning);

85
    // warning label when transparency is not available
Kurt Hindenburg's avatar
Kurt Hindenburg committed
86
    if (KWindowSystem::compositingActive()) {
87
        _ui->transparencyWarningWidget->setVisible(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
88
    } else {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
89
        _ui->transparencyWarningWidget->setText(i18nc("@info:status",
Kurt Hindenburg's avatar
Kurt Hindenburg committed
90 91 92
                                                "The background transparency setting will not"
                                                " be used because your desktop does not appear to support"
                                                " transparent windows."));
93
    }
94
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
95
void ColorSchemeEditor::setRandomizedBackgroundColor(bool randomize)
96 97
{
    _colors->setRandomizedBackgroundColor(randomize);
98 99 100 101 102 103
}
ColorSchemeEditor::~ColorSchemeEditor()
{
    delete _colors;
    delete _ui;
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
104
void ColorSchemeEditor::editColorItem(QTableWidgetItem* item)
105 106
{
    // ignore if this is not a color column
Kurt Hindenburg's avatar
Kurt Hindenburg committed
107
    if (item->column() != 1)
108 109
        return;

110
    QColor color = item->background().color();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
111 112 113
    int result = KColorDialog::getColor(color);
    if (result == KColorDialog::Accepted) {
        item->setBackground(color);
114

Kurt Hindenburg's avatar
Kurt Hindenburg committed
115
        ColorEntry entry(_colors->colorEntry(item->row()));
116
        entry.color = color;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
117
        _colors->setColorTableEntry(item->row(), entry);
118

Kurt Hindenburg's avatar
Kurt Hindenburg committed
119
        emit colorsChanged(_colors);
120

121
    }
122
}
123 124
void ColorSchemeEditor::selectWallpaper()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
125 126 127
    const KUrl url = KFileDialog::getImageOpenUrl(_ui->wallpaperPath->text(),
                     this,
                     i18nc("@action:button", "Select wallpaper image file"));
128

Kurt Hindenburg's avatar
Kurt Hindenburg committed
129
    if (!url.isEmpty())
130 131 132 133 134 135 136 137 138 139 140 141 142
        _ui->wallpaperPath->setText(url.path());
}
void ColorSchemeEditor::wallpaperPathChanged(const QString& path)
{
    if (path.isEmpty())
        _colors->setWallpaper(path);
    else {
        QFileInfo i(path);

        if (i.exists() && i.isFile() && i.isReadable())
            _colors->setWallpaper(path);
    }
}
143 144
void ColorSchemeEditor::setDescription(const QString& text)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
145
    if (_colors)
146 147
        _colors->setDescription(text);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
148
    if (_ui->descriptionEdit->text() != text)
149 150 151 152
        _ui->descriptionEdit->setText(text);
}
void ColorSchemeEditor::setTransparencyPercentLabel(int percent)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
153
    _ui->transparencyPercentLabel->setText(QString("%1%").arg(percent));
154

Kurt Hindenburg's avatar
Kurt Hindenburg committed
155
    qreal opacity = (100.0 - percent) / 100.0;
156 157 158 159
    _colors->setOpacity(opacity);
}
void ColorSchemeEditor::setup(const ColorScheme* scheme)
{
160
    delete _colors;
161 162 163 164 165 166 167 168 169 170

    _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
171
    const int transparencyPercent = qRound((1 - _colors->opacity()) * 100);
172

173 174
    _ui->transparencySlider->setValue(transparencyPercent);
    setTransparencyPercentLabel(transparencyPercent);
175 176

    // randomized background color checkbox
Kurt Hindenburg's avatar
Kurt Hindenburg committed
177
    _ui->randomizedBackgroundCheck->setChecked(scheme->randomizedBackgroundColor());
178 179 180 181

    // wallpaper stuff
    KUrlCompletion* fileCompletion = new KUrlCompletion(KUrlCompletion::FileCompletion);
    fileCompletion->setParent(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
182
    _ui->wallpaperPath->setCompletionObject(fileCompletion);
183
    _ui->wallpaperPath->setClearButtonShown(true);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
184 185
    _ui->wallpaperSelectButton->setIcon(KIcon("image-x-generic"));
    _ui->wallpaperPath->setText(scheme->wallpaper()->path());
186

Kurt Hindenburg's avatar
Kurt Hindenburg committed
187 188 189 190
    connect(_ui->wallpaperSelectButton, SIGNAL(clicked()),
            this, SLOT(selectWallpaper()));
    connect(_ui->wallpaperPath, SIGNAL(textChanged(QString)),
            this, SLOT(wallpaperPathChanged(QString)));
191 192 193
}
void ColorSchemeEditor::setupColorTable(const ColorScheme* colors)
{
194 195
    ColorEntry table[TABLE_COLORS];
    colors->getColorTable(table);
196

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

201
        QTableWidgetItem* colorItem = new QTableWidgetItem();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
202 203
        colorItem->setBackground(table[row].color);
        colorItem->setFlags(colorItem->flags() & ~Qt::ItemIsEditable & ~Qt::ItemIsSelectable);
204
        colorItem->setToolTip(i18n("Click to choose different color"));
205

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

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

#include "ColorSchemeEditor.moc"