Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit f18a8bba authored by Robert Knight's avatar Robert Knight

Add dialog for editing color schemes. Implement basic editing, applying...

Add dialog for editing color schemes.  Implement basic editing, applying changes and saving of color schemes.

svn path=/branches/work/konsole-split-view/; revision=660858
parent 329c063d
......@@ -57,6 +57,7 @@ set(konsole_KDEINIT_SRCS
${sessionadaptors_SRCS}
BlockArray.cpp
ColorScheme.cpp
ColorSchemeEditor.cpp
EditProfileDialog.cpp
EditTabTitleFormatDialog.cpp
Filter.cpp
......@@ -96,6 +97,7 @@ set(konsole_KDEINIT_SRCS
kde4_add_ui_files(konsole_KDEINIT_SRCS
ColorSchemeEditor.ui
EditProfileDialog.ui
EditTabTitleFormatDialog.ui
ManageProfilesDialog.ui
......@@ -129,6 +131,7 @@ set(konsolepart_PART_SRCS
BlockArray.cpp
BookmarkHandler.cpp
ColorScheme.cpp
ColorSchemeEditor.cpp
EditProfileDialog.cpp
EditTabTitleFormatDialog.cpp
Emulation.cpp
......
......@@ -91,8 +91,21 @@ ColorSchemeManager* ColorSchemeManager::_instance = 0;
ColorScheme::ColorScheme()
{
_table = 0;
_opacity = 1.0;
}
ColorScheme::ColorScheme(const ColorScheme& other)
: _opacity(other._opacity)
,_table(0)
{
setName(other.name());
setDescription(other.description());
if ( other._table != 0 )
{
for ( int i = 0 ; i < TABLE_COLORS ; i++ )
setColorTableEntry(i,other._table[i]);
}
}
ColorScheme::~ColorScheme()
{
delete[] _table;
......@@ -349,6 +362,16 @@ bool ColorSchemeManager::loadKDE3ColorScheme(const QString& filePath)
return true;
}
void ColorSchemeManager::addColorScheme(ColorScheme* scheme)
{
_colorSchemes.insert(scheme->name(),scheme);
// save changes to disk
QString path = KGlobal::dirs()->saveLocation("data","konsole/") + scheme->name() + ".colorscheme";
KConfig config(path , KConfig::NoGlobals);
scheme->write(config);
}
bool ColorSchemeManager::loadColorScheme(const QString& filePath)
{
QFileInfo info(filePath);
......@@ -362,13 +385,13 @@ bool ColorSchemeManager::loadColorScheme(const QString& filePath)
Q_ASSERT( !scheme->name().isEmpty() );
qDebug() << "found KDE 4 native color scheme - " << scheme->name();
qDebug() << "opacity - " << scheme->opacity();
if ( !_colorSchemes.contains(info.baseName()) )
{
qDebug() << "added color scheme - " << info.baseName();
_colorSchemes.insert(scheme->name(),scheme);
addColorScheme(scheme);
}
else
{
......
......@@ -27,6 +27,7 @@
#include <QList>
#include <QMetaType>
#include <QIODevice>
#include <QSet>
// Konsole
#include "TECommon.h"
......@@ -51,6 +52,7 @@ public:
* for Konsole.
*/
ColorScheme();
ColorScheme(const ColorScheme& other);
~ColorScheme();
/** Sets the descriptive name of the color scheme. */
......@@ -113,6 +115,7 @@ public:
//void setCursorShape(int shape);
//int cursorShape() const;
static QString colorNameForIndex(int index);
private:
// reads a single colour entry from a KConfig source
ColorEntry readColorEntry(KConfig& config , const QString& colorName);
......@@ -125,7 +128,6 @@ private:
ColorEntry* _table; // pointer to custom color table or 0 if the default
// color scheme is being used
static QString colorNameForIndex(int index);
static const char* colorNames[TABLE_COLORS];
static const ColorEntry defaultTable[]; // table of default color entries
};
......@@ -181,6 +183,9 @@ public:
* requested via a call to findColorScheme()
*/
ColorSchemeManager();
/**
* Destroys the ColorSchemeManager and saves any modified color schemes to disk.
*/
~ColorSchemeManager();
/**
......@@ -198,6 +203,14 @@ public:
*/
const ColorScheme* findColorScheme(const QString& name);
/**
* Adds a new color scheme to the manager. If @p scheme has the same name as
* an existing color scheme, it replaces the existing scheme.
*
* TODO - Ensure the old color scheme gets deleted
*/
void addColorScheme(ColorScheme* scheme);
/**
* Returns a list of the all the available color schemes.
* This may be slow when first called because all of the color
......@@ -226,6 +239,7 @@ private:
void loadAllColorSchemes();
QHash<QString,ColorScheme*> _colorSchemes;
QSet<ColorScheme*> _modifiedSchemes;
bool _haveLoadedAll;
......
/*
Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
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.
*/
// Qt
#include <QBrush>
#include <QFontMetrics>
#include <QHeaderView>
#include <QItemDelegate>
#include <QItemEditorCreatorBase>
#include <QItemEditorFactory>
// KDE
#include <KColorDialog>
// Konsole
#include "ColorSchemeEditor.h"
#include "ui_ColorSchemeEditor.h"
#include "TECommon.h"
using namespace Konsole;
#if 0
class ColorEditorCreator : public QItemEditorCreatorBase
{
virtual QWidget* createWidget(QWidget* parent) const
{
return new KColorButton(parent);
}
virtual QByteArray valuePropertyName() const
{
return QByteArray("color");
}
};
#endif
ColorSchemeEditor::ColorSchemeEditor(QWidget* parent)
: QWidget(parent)
, _colors(0)
{
_ui = new Ui::ColorSchemeEditor();
_ui->setupUi(this);
// description edit
connect( _ui->descriptionEdit , SIGNAL(textChanged(const QString&)) , this ,
SLOT(setDescription(const QString&)) );
// transparency slider
QFontMetrics metrics(font());
_ui->transparencyPercentLabel->setMinimumWidth( metrics.width("100%") );
connect( _ui->transparencySlider , SIGNAL(valueChanged(int)) , this , SLOT(setTransparencyPercentLabel(int)) );
// color table
_ui->colorTable->setColumnCount(2);
_ui->colorTable->setRowCount(TABLE_COLORS);
QStringList labels;
labels << i18n("Name") << i18n("Color");
_ui->colorTable->setHorizontalHeaderLabels(labels);
_ui->colorTable->horizontalHeader()->setStretchLastSection(true);
QTableWidgetItem* item = new QTableWidgetItem("Test");
_ui->colorTable->setItem(0,0,item);
_ui->colorTable->verticalHeader()->hide();
connect( _ui->colorTable , SIGNAL(itemClicked(QTableWidgetItem*)) , this ,
SLOT(editColorItem(QTableWidgetItem*)) );
#if 0
//QItemDelegate* delegate = qobject_cast<QItemDelegate*>(_ui->colorTable->itemDelegateForColumn(1));
QItemDelegate* delegate = new QItemDelegate(this);
_ui->colorTable->setItemDelegateForColumn(1,delegate);
delegate->setItemEditorFactory( new QItemEditorFactory );
Q_ASSERT(delegate);
Q_ASSERT(delegate->itemEditorFactory());
delegate->itemEditorFactory()->registerEditor( QVariant::Color , new ColorEditorCreator );
#endif
}
ColorSchemeEditor::~ColorSchemeEditor()
{
delete _colors;
delete _ui;
}
void ColorSchemeEditor::editColorItem( QTableWidgetItem* item )
{
// ignore if this is not a color column
if ( item->column() != 1 )
return;
KColorDialog* dialog = new KColorDialog(this);
dialog->setColor( item->background().color() );
dialog->exec();
item->setBackground( dialog->color() );
ColorEntry entry(_colors->colorTable()[item->row()]);
entry.color = dialog->color();
_colors->setColorTableEntry(item->row(),entry);
}
void ColorSchemeEditor::setDescription(const QString& text)
{
if ( _colors )
_colors->setDescription(text);
if ( _ui->descriptionEdit->text() != text )
_ui->descriptionEdit->setText(text);
}
void ColorSchemeEditor::setTransparencyPercentLabel(int percent)
{
_ui->transparencyPercentLabel->setText( QString("%1\%").arg(percent) );
qreal opacity = ( 100.0 - percent ) / 100.0;
_colors->setOpacity(opacity);
qDebug() << "set opacity to:" << opacity;
}
void ColorSchemeEditor::setup(const ColorScheme* scheme)
{
if ( _colors )
delete _colors;
_colors = new ColorScheme(*scheme);
// setup description edit
_ui->descriptionEdit->setText(_colors->description());
// setup color table
setupColorTable(_colors);
// setup transparency slider
qDebug() << "read opacity: " << _colors->opacity();
const int transparencyPercent = (int) ( (1-_colors->opacity())*100 );
_ui->transparencySlider->setValue(transparencyPercent);
setTransparencyPercentLabel(transparencyPercent);
}
void ColorSchemeEditor::setupColorTable(const ColorScheme* colors)
{
const ColorEntry* table = colors->colorTable();
for ( int row = 0 ; row < TABLE_COLORS ; row++ )
{
QTableWidgetItem* nameItem = new QTableWidgetItem( colors->colorNameForIndex(row) );
QTableWidgetItem* colorItem = new QTableWidgetItem();
colorItem->setBackground( table[row].color );
colorItem->setFlags( colorItem->flags() & ~Qt::ItemIsEditable & ~Qt::ItemIsSelectable );
_ui->colorTable->setItem(row,0,nameItem);
_ui->colorTable->setItem(row,1,colorItem);
}
}
ColorScheme* ColorSchemeEditor::colorScheme() const
{
return _colors;
}
#include "ColorSchemeEditor.moc"
/*
Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
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.
*/
#ifndef COLORSCHEMEEDITOR_H
#define COLORSCHEMEEDITOR_H
// Qt
#include <QWidget>
// Konsole
#include "ColorScheme.h"
class QTableWidgetItem;
namespace Ui
{
class ColorSchemeEditor;
};
namespace Konsole
{
class ColorScheme;
/**
* A dialog for editing color schemes.
*/
class ColorSchemeEditor : public QWidget
{
Q_OBJECT
public:
/** Constructs a new color scheme editor with the specified parent. */
ColorSchemeEditor(QWidget* parent = 0);
virtual ~ColorSchemeEditor();
/** Initialises the dialog with the properties of the specified color scheme. */
void setup(const ColorScheme* scheme);
/** Returns the edited color scheme. */
ColorScheme* colorScheme() const;
public slots:
/** Sets the text displayed in the description edit field. */
void setDescription(const QString& description);
private slots:
void setTransparencyPercentLabel(int percent);
void editColorItem(QTableWidgetItem* item);
private:
void setupColorTable(const ColorScheme* table);
Ui::ColorSchemeEditor* _ui;
ColorScheme* _colors;
};
};
#endif // COLORSCHEMEEDITOR_H
<ui version="4.0" >
<class>ColorSchemeEditor</class>
<widget class="QWidget" name="ColorSchemeEditor" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>257</width>
<height>169</height>
</rect>
</property>
<property name="windowTitle" >
<string>Form</string>
</property>
<layout class="QVBoxLayout" >
<item>
<layout class="QHBoxLayout" >
<item>
<widget class="QLabel" name="label" >
<property name="text" >
<string>Description:</string>
</property>
<property name="alignment" >
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="descriptionEdit" />
</item>
</layout>
</item>
<item>
<widget class="QTableWidget" name="colorTable" />
</item>
<item>
<layout class="QHBoxLayout" >
<item>
<widget class="QLabel" name="label_2" >
<property name="text" >
<string>Background Transparency:</string>
</property>
<property name="alignment" >
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QSlider" name="transparencySlider" >
<property name="maximum" >
<number>100</number>
</property>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="transparencyPercentLabel" >
<property name="text" >
<string>Percent</string>
</property>
<property name="alignment" >
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
......@@ -32,6 +32,7 @@
// Konsole
#include "ColorScheme.h"
#include "ColorSchemeEditor.h"
#include "ui_EditProfileDialog.h"
#include "EditProfileDialog.h"
#include "EditTabTitleFormatDialog.h"
......@@ -239,7 +240,8 @@ void EditProfileDialog::setupAppearencePage(const Profile* info)
ColorScheme* colors = schemeIter.next();
QStandardItem* item = new QStandardItem(colors->description());
item->setData( QVariant::fromValue(colors) , Qt::UserRole + 1);
//item->setFlags( item->flags() | Qt::ItemIsUserCheckable );
//item->setCheckState( Qt::Unchecked );
model->appendRow(item);
}
......@@ -250,6 +252,12 @@ void EditProfileDialog::setupAppearencePage(const Profile* info)
SLOT(colorSchemeSelected()) );
connect( _ui->selectColorSchemeButton , SIGNAL(clicked()) , this ,
SLOT(colorSchemeSelected()) );
connect( _ui->editColorSchemeButton , SIGNAL(clicked()) , this ,
SLOT(editColorScheme()) );
connect( _ui->removeColorSchemeButton , SIGNAL(clicked()) , this ,
SLOT(removeColorScheme()) );
connect( _ui->newColorSchemeButton , SIGNAL(clicked()) , this ,
SLOT(newColorScheme()) );
// setup font preview
const QFont& font = info->font();
......@@ -261,6 +269,61 @@ void EditProfileDialog::setupAppearencePage(const Profile* info)
connect( _ui->editFontButton , SIGNAL(clicked()) , this ,
SLOT(showFontDialog()) );
}
void EditProfileDialog::removeColorScheme()
{
//ColorSchemeManager::instance()->deleteColorScheme(colors);
}
void EditProfileDialog::showColorSchemeEditor(bool newScheme)
{
QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();
if ( !selected.isEmpty() )
{
QAbstractItemModel* model = _ui->colorSchemeList->model();
QModelIndex index = selected.first();
ColorScheme* colors = model->data(index,Qt::UserRole+1).value<ColorScheme*>();
KDialog* dialog = new KDialog(this);
if ( newScheme )
dialog->setCaption(i18n("New Color Scheme"));
else
dialog->setCaption(i18n("Edit Color Scheme"));
ColorSchemeEditor* editor = new ColorSchemeEditor;
dialog->setMainWidget(editor);
editor->setup(colors);
if ( newScheme )
editor->setDescription(i18n("New Color Scheme"));
if ( dialog->exec() == QDialog::Accepted )
{
ColorScheme* newScheme = new ColorScheme(*editor->colorScheme());
ColorSchemeManager::instance()->addColorScheme( newScheme );
model->setData(index, QVariant::fromValue(newScheme) , Qt::UserRole+1);
model->setData(index, newScheme->description() , Qt::DisplayRole);
const QString& currentScheme = SessionManager::instance()->profile(_profileKey)->colorScheme();
// the next couple of lines may seem slightly odd,
// but they force any open views based on the current profile
// to update their color schemes
if ( newScheme->name() == currentScheme )
{
_tempProfile->setProperty(Profile::ColorScheme,newScheme->name());
}
}
}
}
void EditProfileDialog::newColorScheme()
{
showColorSchemeEditor(true);
}
void EditProfileDialog::editColorScheme()
{
showColorSchemeEditor(false);
}
void EditProfileDialog::colorSchemeSelected()
{
QModelIndexList selected = _ui->colorSchemeList->selectionModel()->selectedIndexes();
......@@ -470,6 +533,35 @@ ColorSchemeViewDelegate::ColorSchemeViewDelegate(QObject* parent)
{
}
#if 0
QWidget* ColorSchemeViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QWidget* holder = new QWidget(parent);
QVBoxLayout* layout = new QVBoxLayout;
QWidget* selectButton = new QPushButton(i18n("Use Color Scheme"));
QWidget* editButton = new QPushButton(i18n("Edit..."));
layout->setMargin(0);
layout->addWidget(selectButton);
layout->addWidget(editButton);
holder->setLayout(layout);
int width = holder->sizeHint().width();
int left = option.rect.right() - width - 10;
int top = option.rect.top();
holder->move( left , top );
return holder;
}
#endif
void ColorSchemeViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
......@@ -477,14 +569,10 @@ void ColorSchemeViewDelegate::paint(QPainter* painter, const QStyleOptionViewIte
Q_ASSERT(scheme);
QLinearGradient gradient(option.rect.topLeft(),option.rect.bottomRight());
gradient.setColorAt(0,scheme->foregroundColor());
gradient.setColorAt(1,scheme->backgroundColor());
QBrush brush(scheme->backgroundColor());
painter->fillRect( option.rect , brush );
const ColorEntry* entries = scheme->colorTable();
/* const ColorEntry* entries = scheme->colorTable();
const qreal colorRectWidth = qMin(option.rect.width(),256) / TABLE_COLORS;
const qreal colorRectHeight = colorRectWidth;
qreal x = 0;
......@@ -499,7 +587,7 @@ void ColorSchemeViewDelegate::paint(QPainter* painter, const QStyleOptionViewIte
painter->fillRect( colorRect , QColor(entries[i].color));
x += colorRectWidth;
}
}*/
QPen pen(scheme->foregroundColor());
painter->setPen(pen);
......
......@@ -23,6 +23,7 @@
// Qt
#include <QAbstractItemDelegate>
#include <QPair>
#include <QSet>
// KDE
#include <KDialog>
......@@ -40,7 +41,7 @@ namespace Konsole
class Profile;
/**
* A dialog which allows the user to edit a session type.
* A dialog which allows the user to edit a profile.
*
* TODO: More documentation
*/
......@@ -92,6 +93,9 @@ private slots:
// appearence page
void setFontSize(int pointSize);
void showFontDialog();
void newColorScheme();
void editColorScheme();
void removeColorScheme();
void colorSchemeSelected();
// scrolling page
......@@ -125,6 +129,8 @@ private:
void setupScrollingPage(const Profile* info);
void setupAdvancedPage(const Profile* info);
void showColorSchemeEditor(bool newScheme);
struct RadioOption
{
QAbstractButton* button;
......@@ -159,8 +165,11 @@ public:
virtual QSize sizeHint( const QStyleOptionViewItem& option,
const QModelIndex& index) const;
// virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option,
// const QModelIndex& index) const;
// virtual bool editorEvent(QEvent* event,QAbstractItemModel* model,
// const QStyleOptionViewItem& option, const QModelIndex& index);
};
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment