Commit 50fb0ef4 authored by Robert Knight's avatar Robert Knight

Support for per-session randomized color schemes. Mainly useful to allow each...

Support for per-session randomized color schemes.  Mainly useful to allow each new session (from a particular profile) to have a different background color to make them easier to identify at a glance.  Randomization is specified in the color scheme as the maximum amount of difference in hue, saturation and value which a color is allowed to be randomly changed by.  No GUI controls for this yet.  Added a 'Black on Random Light' color scheme to make use of this facility out of the box.

svn path=/trunk/KDE/kdebase/apps/konsole/; revision=682403
parent 44962b4e
[Background]
Bold=false
Color=247,247,214
Transparency=true
MaxRandomHue=340
[BackgroundIntense]
Bold=false
Color=255,255,221
Transparency=true
[Color0]
Bold=false
Color=0,0,0
Transparency=false
[Color0Intense]
Bold=false
Color=104,104,104
Transparency=false
[Color1]
Bold=false
Color=178,24,24
Transparency=false
[Color1Intense]
Bold=false
Color=255,84,84
Transparency=false
[Color2]
Bold=false
Color=24,178,24
Transparency=false
[Color2Intense]
Bold=false
Color=84,255,84
Transparency=false
[Color3]
Bold=false
Color=178,104,24
Transparency=false
[Color3Intense]
Bold=false
Color=255,255,84
Transparency=false
[Color4]
Bold=false
Color=24,24,178
Transparency=false
[Color4Intense]
Bold=false
Color=84,84,255
Transparency=false
[Color5]
Bold=false
Color=178,24,178
Transparency=false
[Color5Intense]
Bold=false
Color=255,84,255
Transparency=false
[Color6]
Bold=false
Color=24,178,178
Transparency=false
[Color6Intense]
Bold=false
Color=84,255,255
Transparency=false
[Color7]
Bold=false
Color=178,178,178
Transparency=false
[Color7Intense]
Bold=false
Color=255,255,255
Transparency=false
[Foreground]
Bold=false
Color=0,0,0
Transparency=false
[ForegroundIntense]
Bold=true
Color=0,0,0
Transparency=false
[General]
Description=Black on Random Light
Opacity=1
......@@ -2,7 +2,8 @@
# at present
install( FILES
BlackOnLightYellow.schema
BlackOnLightYellow.schema
BlackOnRandomLight.colorscheme
BlackOnWhite.schema
GreenOnBlack.colorscheme
WhiteOnBlack.schema
......
# Hello world
Id=kde2.2/r1
File=konsolerc
Group=options,Desktop Entry
Key=has frame
Key=schema
Key=defaultfont
Key=MenuBar
Key=scrollbar
Id=kde3.0/r1
File=konsolerc
Options=overwrite
Script=schemaStrip.pl,perl
#!/usr/bin/perl
foreach (<>) {
if(/^schema=.*\/(.*)$/) {
print "schema=$1\n";
next;
}
print $_;
}
......@@ -113,11 +113,13 @@ ColorSchemeManager* ColorSchemeManager::_instance = 0;
ColorScheme::ColorScheme()
{
_table = 0;
_randomTable = 0;
_opacity = 1.0;
}
ColorScheme::ColorScheme(const ColorScheme& other)
: _opacity(other._opacity)
,_table(0)
,_randomTable(0)
{
setName(other.name());
setDescription(other.description());
......@@ -127,10 +129,20 @@ ColorScheme::ColorScheme(const ColorScheme& other)
for ( int i = 0 ; i < TABLE_COLORS ; i++ )
setColorTableEntry(i,other._table[i]);
}
if ( other._randomTable != 0 )
{
for ( int i = 0 ; i < TABLE_COLORS ; i++ )
{
const RandomizationRange& range = other._randomTable[i];
setRandomizationRange(i,range.hue,range.saturation,range.value);
}
}
}
ColorScheme::~ColorScheme()
{
delete[] _table;
delete[] _randomTable;
}
void ColorScheme::setDescription(const QString& description) { _description = description; }
......@@ -148,6 +160,64 @@ void ColorScheme::setColorTableEntry(int index , const ColorEntry& entry)
_table[index] = entry;
}
ColorEntry ColorScheme::colorEntry(int index , uint randomSeed) const
{
Q_ASSERT( index >= 0 && index < TABLE_COLORS );
if ( randomSeed != 0 )
qsrand(randomSeed);
ColorEntry entry = colorTable()[index];
// qDebug() << "Old color: " << entry.color;
// qDebug() << "Random seed: " << randomSeed << "random table: " <<
// _randomTable << "isnull: " << ((_randomTable) ? _randomTable[index].isNull() : true);
if ( randomSeed != 0 &&
_randomTable != 0 &&
!_randomTable[index].isNull() )
{
const RandomizationRange& range = _randomTable[index];
int hueDifference = range.hue ? (qrand() % range.hue) - range.hue/2 : 0;
int saturationDifference = range.saturation ? (qrand() % range.saturation) - range.saturation/2 : 0;
int valueDifference = range.value ? (qrand() % range.value) - range.value/2 : 0;
QColor& color = entry.color;
int newHue = qAbs( (color.hue() + hueDifference) % MAX_HUE );
int newValue = qAbs( color.value() + valueDifference );
int newSaturation = qAbs( color.saturation() + saturationDifference );
// qDebug() << "New hue: " << newHue << "New sat:" << newSaturation <<
// "New value:" << newValue;
color.setHsv(newHue,newSaturation,newValue);
}
// qDebug() << "New color: " << entry.color;
return entry;
}
void ColorScheme::getColorTable(ColorEntry* table , uint randomSeed) const
{
for ( int i = 0 ; i < TABLE_COLORS ; i++ )
table[i] = colorEntry(i,randomSeed);
}
void ColorScheme::setRandomizationRange( int index , quint16 hue , quint8 saturation ,
quint8 value )
{
Q_ASSERT( hue <= MAX_HUE );
Q_ASSERT( index >= 0 && index < TABLE_COLORS );
if ( _randomTable == 0 )
_randomTable = new RandomizationRange[TABLE_COLORS];
_randomTable[index].hue = hue;
_randomTable[index].value = value;
_randomTable[index].saturation = saturation;
}
const ColorEntry* ColorScheme::colorTable() const
{
if ( _table )
......@@ -175,7 +245,7 @@ void ColorScheme::read(KConfig& config)
for (int i=0 ; i < TABLE_COLORS ; i++)
{
setColorTableEntry(i,readColorEntry(config,colorNameForIndex(i)));
readColorEntry(config,i);
}
}
void ColorScheme::write(KConfig& config) const
......@@ -203,9 +273,9 @@ QString ColorScheme::translatedColorNameForIndex(int index)
return QString(translatedColorNames[index]);
}
ColorEntry ColorScheme::readColorEntry(KConfig& config , const QString& colorName)
void ColorScheme::readColorEntry(KConfig& config , int index)
{
KConfigGroup configGroup(&config,colorName);
KConfigGroup configGroup(&config,colorNameForIndex(index));
ColorEntry entry;
......@@ -213,7 +283,14 @@ ColorEntry ColorScheme::readColorEntry(KConfig& config , const QString& colorNam
entry.transparent = configGroup.readEntry("Transparent",false);
entry.bold = configGroup.readEntry("Bold",false);
return entry;
quint16 hue = configGroup.readEntry("MaxRandomHue",0);
quint8 value = configGroup.readEntry("MaxRandomValue",0);
quint8 saturation = configGroup.readEntry("MaxRandomSaturation",0);
setColorTableEntry( index , entry );
if ( hue != 0 || value != 0 || saturation != 0 )
setRandomizationRange( index , hue , saturation , value );
}
void ColorScheme::writeColorEntry(KConfig& config , const QString& colorName, const ColorEntry& entry) const
{
......
......@@ -72,14 +72,24 @@ public:
/** Sets a single entry within the color palette. */
void setColorTableEntry(int index , const ColorEntry& entry);
/**
* Returns a pointer to an array of color entries which form the palette
* for this color scheme.
* The array has TABLE_COLORS entries
* Copies the color entries which form the palette for this color scheme
* into @p table. @p table should be an array with TABLE_COLORS entries.
*
* TODO: More documentation
* @param table Array into which the color entries for this color scheme
* are copied.
* @param randomSeed Color schemes may allow certain colors in their
* palette to be randomized. The seed is used to pick the random color.
*/
const ColorEntry* colorTable() const;
void getColorTable(ColorEntry* table, uint randomSeed = 0) const;
/**
* Retrieves a single color entry from the table.
*
* See getColorTable()
*/
ColorEntry colorEntry(int index , uint randomSeed = 0) const;
/**
* Convenience method. Returns the
......@@ -121,17 +131,49 @@ public:
static QString translatedColorNameForIndex(int index);
private:
// returns the active color table. if none has been set specifically,
// this is the default color table.
const ColorEntry* colorTable() const;
// reads a single colour entry from a KConfig source
ColorEntry readColorEntry(KConfig& config , const QString& colorName);
// and sets the the palette entry at 'index' to the entry read.
void readColorEntry(KConfig& config , int index);
// writes a single colour entry to a KConfig source
void writeColorEntry(KConfig& config , const QString& colorName, const ColorEntry& entry) const;
// sets the amount of randomization allowed for a particular color
// in the palette. creates the randomization table if
// it does not already exist
void setRandomizationRange( int index , quint16 hue , quint8 saturation , quint8 value );
QString _description;
QString _name;
qreal _opacity;
ColorEntry* _table; // pointer to custom color table or 0 if the default
// color scheme is being used
// specifies how much a particular color can be randomized by
class RandomizationRange
{
public:
RandomizationRange() : hue(0) , saturation(0) , value(0) {}
bool isNull() const
{
return ( hue == 0 && saturation == 0 && value == 0 );
}
quint16 hue;
quint8 saturation;
quint8 value;
};
static const quint16 MAX_HUE = 340;
RandomizationRange* _randomTable; // pointer to randomization table or 0
// if no colors in the color scheme support
// randomization
static const char* colorNames[TABLE_COLORS];
static const QString translatedColorNames[TABLE_COLORS];
......
......@@ -132,7 +132,7 @@ void ColorSchemeEditor::editColorItem( QTableWidgetItem* item )
item->setBackground( dialog->color() );
ColorEntry entry(_colors->colorTable()[item->row()]);
ColorEntry entry(_colors->colorEntry(item->row()));
entry.color = dialog->color();
_colors->setColorTableEntry(item->row(),entry);
......@@ -177,7 +177,8 @@ void ColorSchemeEditor::setup(const ColorScheme* scheme)
}
void ColorSchemeEditor::setupColorTable(const ColorScheme* colors)
{
const ColorEntry* table = colors->colorTable();
ColorEntry table[TABLE_COLORS];
colors->getColorTable(table);
for ( int row = 0 ; row < TABLE_COLORS ; row++ )
{
......
......@@ -206,7 +206,6 @@ void Session::addView(TerminalDisplay* widget)
QObject::connect( widget ,SIGNAL(destroyed(QObject*)) , this ,
SLOT(viewDestroyed(QObject*)) );
}
void Session::viewDestroyed(QObject* view)
......
......@@ -122,22 +122,6 @@ void TerminalDisplay::setScreenWindow(ScreenWindow* window)
}
}
void TerminalDisplay::setDefaultBackColor(const QColor& color)
{
_defaultBgColor = color;
QPalette p = palette();
p.setColor( backgroundRole(), defaultBackColor() );
setPalette( p );
}
QColor TerminalDisplay::defaultBackColor()
{
if (_defaultBgColor.isValid())
return _defaultBgColor;
return _colorTable[DEFAULT_BACK_COLOR].color;
}
const ColorEntry* TerminalDisplay::colorTable() const
{
return _colorTable;
......@@ -149,7 +133,7 @@ void TerminalDisplay::setColorTable(const ColorEntry table[])
_colorTable[i] = table[i];
QPalette p = palette();
p.setColor( backgroundRole(), defaultBackColor() );
p.setColor( backgroundRole(), _colorTable[DEFAULT_BACK_COLOR].color );
setPalette( p );
update();
......@@ -264,6 +248,7 @@ TerminalDisplay::TerminalDisplay(QWidget *parent)
,_contentHeight(1)
,_contentWidth(1)
,_image(0)
,_randomSeed(0)
,_resizing(false)
,_terminalSizeHint(false)
,_terminalSizeStartup(true)
......@@ -665,7 +650,7 @@ void TerminalDisplay::drawTextFragment(QPainter& painter ,
const QColor backgroundColor = style->backgroundColor.color(_colorTable);
// draw background if different from the display's background color
if ( backgroundColor != defaultBackColor() )
if ( backgroundColor != palette().background().color() )
drawBackground(painter,rect,backgroundColor);
// draw cursor shape if the current character is the cursor
......@@ -680,6 +665,9 @@ void TerminalDisplay::drawTextFragment(QPainter& painter ,
painter.restore();
}
void TerminalDisplay::setRandomSeed(uint randomSeed) { _randomSeed = randomSeed; }
uint TerminalDisplay::randomSeed() const { return _randomSeed; }
/*!
Set XIM Position
*/
......@@ -1091,7 +1079,7 @@ void TerminalDisplay::paintEvent( QPaintEvent* pe )
foreach (QRect rect, (pe->region() & contentsRect()).rects())
{
drawBackground(paint,rect,defaultBackColor());
drawBackground(paint,rect,palette().background().color());
paintContents(paint, rect);
}
paintFilters(paint);
......
......@@ -71,15 +71,15 @@ public:
TerminalDisplay(QWidget *parent=0);
virtual ~TerminalDisplay();
/** Sets the default background color for the display. */
void setDefaultBackColor(const QColor& color);
/** Returns the default background color for the display */
QColor defaultBackColor();
/** Returns the terminal color palette used by the display. */
const ColorEntry* colorTable() const;
/** Sets the terminal color palette used by the display. */
void setColorTable(const ColorEntry table[]);
/** TODO: Document me */
void setRandomSeed(uint seed);
/** TODO: Document me */
uint randomSeed() const;
/** Sets the opacity of the terminal display. */
void setOpacity(qreal opacity);
......@@ -614,7 +614,7 @@ private:
QVector<LineProperty> _lineProperties;
ColorEntry _colorTable[TABLE_COLORS];
QColor _defaultBgColor;
uint _randomSeed;
bool _resizing;
bool _terminalSizeHint;
......
......@@ -24,6 +24,7 @@
#include <assert.h>
// Qt
#include <QtCore/QDateTime>
#include <QtCore/QSignalMapper>
// KDE
......@@ -328,7 +329,7 @@ void ViewManager::splitView(Qt::Orientation orientation)
while (existingViewIter.hasNext())
{
Session* session = _sessionMap[(TerminalDisplay*)existingViewIter.next()];
TerminalDisplay* display = createTerminalDisplay();
TerminalDisplay* display = createTerminalDisplay(session);
applyProfile(display,session->profileKey());
ViewProperties* properties = createController(session,display);
......@@ -437,7 +438,7 @@ void ViewManager::createView(Session* session)
while ( containerIter.hasNext() )
{
ViewContainer* container = containerIter.next();
TerminalDisplay* display = createTerminalDisplay();
TerminalDisplay* display = createTerminalDisplay(session);
applyProfile(display,session->profileKey());
// set initial size
......@@ -518,7 +519,7 @@ void ViewManager::viewCloseRequest(QWidget* view)
focusActiveView();
}
TerminalDisplay* ViewManager::createTerminalDisplay()
TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
{
TerminalDisplay* display = new TerminalDisplay(0);
......@@ -528,7 +529,8 @@ TerminalDisplay* ViewManager::createTerminalDisplay()
display->setCutToBeginningOfLine(true);
display->setTerminalSizeStartup(false);
display->setScrollBarLocation(TerminalDisplay::SCROLLBAR_RIGHT);
display->setRandomSeed(session->sessionId() * 31);
return display;
}
......@@ -564,8 +566,12 @@ void ViewManager::applyProfile(TerminalDisplay* view , const QString& profileKey
else if ( tabBarPosition == Profile::TabBarBottom )
container->setNavigationPosition(ViewContainer::NavigationPositionBottom);
qDebug() << __FUNCTION__ << "Using random seed:" << view->randomSeed();
// load colour scheme
view->setColorTable(colorScheme->colorTable());
ColorEntry table[TABLE_COLORS];
colorScheme->getColorTable(table , view->randomSeed() );
view->setColorTable(table);
view->setOpacity(colorScheme->opacity());
// load font
......
......@@ -198,7 +198,9 @@ private:
void removeContainer(ViewContainer* container);
// creates a new terminal display
TerminalDisplay* createTerminalDisplay();
// the 'session' is used so that the terminal display's random seed
// can be set to something which depends uniquely on that session
TerminalDisplay* createTerminalDisplay(Session* session = 0);
// creates a new controller for a session/display pair which provides the menu
// actions associated with that view, and exposes basic information
......
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