From 087f67e4fca15761809d24949c4367ec92b75a9b Mon Sep 17 00:00:00 2001 From: Kurt Hindenburg Date: Sun, 7 Aug 2011 18:44:16 -0400 Subject: [PATCH] Allow an image to be set as the background in the terminal. Restore the KDE3 option to have a background image in each session. Patch by Alexandre Becoulet BUG: 157882 FIXED-IN: 4.8 --- src/ColorScheme.cpp | 60 +++++++++++++++++++++++++++++++++++++++ src/ColorScheme.h | 38 ++++++++++++++++++++++++- src/ColorSchemeEditor.cpp | 36 +++++++++++++++++++++++ src/ColorSchemeEditor.h | 2 ++ src/ColorSchemeEditor.ui | 33 ++++++++++++++++++++- src/TerminalDisplay.cpp | 22 +++++++++++--- src/TerminalDisplay.h | 6 ++++ src/ViewManager.cpp | 1 + 8 files changed, 192 insertions(+), 6 deletions(-) diff --git a/src/ColorScheme.cpp b/src/ColorScheme.cpp index 00b085375..c2ea89dfd 100644 --- a/src/ColorScheme.cpp +++ b/src/ColorScheme.cpp @@ -26,6 +26,7 @@ #include #include #include +#include // KDE #include @@ -117,11 +118,14 @@ ColorScheme::ColorScheme() _table = 0; _randomTable = 0; _opacity = 1.0; + setWallpaper(QString()); } + ColorScheme::ColorScheme(const ColorScheme& other) : _opacity(other._opacity) ,_table(0) ,_randomTable(0) + ,_wallpaper(other._wallpaper) { setName(other.name()); setDescription(other.description()); @@ -270,6 +274,7 @@ void ColorScheme::read(KConfig& config) _description = i18n(description.toUtf8()); _opacity = configGroup.readEntry("Opacity",qreal(1.0)); + setWallpaper(configGroup.readEntry("Wallpaper", QString())); for (int i=0 ; i < TABLE_COLORS ; i++) { @@ -282,6 +287,7 @@ void ColorScheme::write(KConfig& config) const configGroup.writeEntry("Description",_description); configGroup.writeEntry("Opacity",_opacity); + configGroup.writeEntry("Wallpaper", _wallpaper->path()); for (int i=0 ; i < TABLE_COLORS ; i++) { @@ -350,6 +356,60 @@ void ColorScheme::writeColorEntry(KConfig& config , const QString& colorName, co } } +void ColorScheme::setWallpaper(const QString& path) +{ + _wallpaper.attach(new ColorSchemeWallpaper(path)); +} + +ColorSchemeWallpaper::Ptr ColorScheme::wallpaper() const +{ + return _wallpaper; +} + +ColorSchemeWallpaper::ColorSchemeWallpaper(const QString& path) + : _path(path), + _picture(0) +{ + // kDebug(1211) << "wallpaper created" << _path << "\n"; +} + +ColorSchemeWallpaper::~ColorSchemeWallpaper() +{ + // kDebug(1211) << "wallpaper deleted" << _path << "\n"; + delete _picture; +} + +void ColorSchemeWallpaper::load() +{ + if (_path.isEmpty()) + return; + + // Create and load original pixmap + if (!_picture) + _picture = new QPixmap(); + + if (_picture->isNull()) + _picture->load(_path); +} + +bool ColorSchemeWallpaper::isNull() const +{ + return _path.isEmpty(); +} + +bool ColorSchemeWallpaper::draw(QPainter& painter, const QRect& rect) +{ + if (!_picture || _picture->isNull()) + return false; + + painter.drawTiledPixmap(rect, *_picture, rect.topLeft()); + return true; +} + +QString ColorSchemeWallpaper::path() const +{ + return _path; +} // // Work In Progress - A color scheme for use on KDE setups for users diff --git a/src/ColorScheme.h b/src/ColorScheme.h index db6cd5140..76b6d021c 100644 --- a/src/ColorScheme.h +++ b/src/ColorScheme.h @@ -29,15 +29,46 @@ #include #include +// KDE +#include + // Konsole #include "CharacterColor.h" class QIODevice; class KConfig; +class QPixmap; +class QPainter; namespace Konsole { +/** + * This class holds the wallpaper pixmap associated with a color scheme. + * The wallpaper object is shared between multiple TerminalDisplay. + */ +class ColorSchemeWallpaper : public QSharedData +{ +public: + typedef KSharedPtr Ptr; + + ColorSchemeWallpaper(const QString& path); + ~ColorSchemeWallpaper(); + + void load(); + + /** Returns true if wallpaper available and drawn */ + bool draw(QPainter& painter, const QRect& rect); + + bool isNull() const; + + QString path() const; + +private: + QString _path; + QPixmap* _picture; +}; + /** * Represents a color scheme for a terminal display. * @@ -128,6 +159,10 @@ public: */ qreal opacity() const; + void setWallpaper(const QString& path); + + ColorSchemeWallpaper::Ptr wallpaper() const; + /** * Enables randomization of the background color. This will cause * the palette returned by getColorTable() and colorEntry() to @@ -180,13 +215,14 @@ private: ColorEntry* _table; // pointer to custom color table or 0 if the default // color scheme is being used - static const quint16 MAX_HUE = 340; RandomizationRange* _randomTable; // pointer to randomization table or 0 // if no colors in the color scheme support // randomization + ColorSchemeWallpaper::Ptr _wallpaper; + static const char* const colorNames[TABLE_COLORS]; static const char* const translatedColorNames[TABLE_COLORS]; diff --git a/src/ColorSchemeEditor.cpp b/src/ColorSchemeEditor.cpp index a2181a573..3cd2bbc76 100644 --- a/src/ColorSchemeEditor.cpp +++ b/src/ColorSchemeEditor.cpp @@ -26,11 +26,14 @@ #include #include #include +#include // KDE #include #include #include +#include +#include // Konsole #include "ui_ColorSchemeEditor.h" @@ -134,6 +137,26 @@ void ColorSchemeEditor::editColorItem( QTableWidgetItem* item ) } } +void ColorSchemeEditor::selectWallpaper() +{ + const KUrl url = KFileDialog::getImageOpenUrl( _ui->wallpaperPath->text(), + this, + i18n("Select wallpaper image file")); + + if ( !url.isEmpty() ) + _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); + } +} void ColorSchemeEditor::setDescription(const QString& text) { if ( _colors ) @@ -169,6 +192,19 @@ void ColorSchemeEditor::setup(const ColorScheme* scheme) // randomized background color checkbox _ui->randomizedBackgroundCheck->setChecked( scheme->randomizedBackgroundColor() ); + + // 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") ); + _ui->wallpaperPath->setText( scheme->wallpaper()->path() ); + + connect( _ui->wallpaperSelectButton, SIGNAL(clicked()), + this, SLOT(selectWallpaper())); + connect( _ui->wallpaperPath, SIGNAL(textChanged(const QString&)), + this, SLOT(wallpaperPathChanged(const QString&))); } void ColorSchemeEditor::setupColorTable(const ColorScheme* colors) { diff --git a/src/ColorSchemeEditor.h b/src/ColorSchemeEditor.h index 5ae6d1d2b..a97a4c773 100644 --- a/src/ColorSchemeEditor.h +++ b/src/ColorSchemeEditor.h @@ -76,6 +76,8 @@ private slots: void setTransparencyPercentLabel(int percent); void setRandomizedBackgroundColor(bool randomized); void editColorItem(QTableWidgetItem* item); + void wallpaperPathChanged(const QString& path); + void selectWallpaper(); private: void setupColorTable(const ColorScheme* table); diff --git a/src/ColorSchemeEditor.ui b/src/ColorSchemeEditor.ui index 8c309a812..042213f84 100644 --- a/src/ColorSchemeEditor.ui +++ b/src/ColorSchemeEditor.ui @@ -1,3 +1,4 @@ + ColorSchemeEditor @@ -6,7 +7,7 @@ 0 0 364 - 475 + 502 @@ -74,6 +75,31 @@ + + + + + + Wallpaper: + + + + + + + The wallpaper picture file path + + + + + + + ... + + + + + @@ -97,6 +123,11 @@ + + KLineEdit + QLineEdit +
klineedit.h
+
Konsole::WarningBox QWidget diff --git a/src/TerminalDisplay.cpp b/src/TerminalDisplay.cpp index 6006b4a14..52e090d63 100644 --- a/src/TerminalDisplay.cpp +++ b/src/TerminalDisplay.cpp @@ -578,6 +578,11 @@ void TerminalDisplay::setOpacity(qreal opacity) _blendColor = color.rgba(); } +void TerminalDisplay::setWallpaper(ColorSchemeWallpaper::Ptr p) +{ + _wallpaper = p; +} + void TerminalDisplay::drawBackground(QPainter& painter, const QRect& rect, const QColor& backgroundColor, bool useOpacitySetting ) { // the area of the widget showing the contents of the terminal display is drawn @@ -594,7 +599,11 @@ void TerminalDisplay::drawBackground(QPainter& painter, const QRect& rect, const QRegion contentsRegion = QRegion(rect).subtracted(scrollBarArea); QRect contentsRect = contentsRegion.boundingRect(); - if ( HAVE_TRANSPARENCY && qAlpha(_blendColor) < 0xff && useOpacitySetting ) + if ( useOpacitySetting && !_wallpaper->isNull() && + _wallpaper->draw(painter, contentsRect) ) + { + } + else if ( HAVE_TRANSPARENCY && qAlpha(_blendColor) < 0xff && useOpacitySetting ) { QColor color(backgroundColor); color.setAlpha(qAlpha(_blendColor)); @@ -939,9 +948,12 @@ void TerminalDisplay::updateImage() // optimization - scroll the existing image where possible and // avoid expensive text drawing for parts of the image that // can simply be moved up or down - scrollImage( _screenWindow->scrollCount() , - _screenWindow->scrollRegion() ); - _screenWindow->resetScrollCount(); + if (_wallpaper->isNull()) + { + scrollImage( _screenWindow->scrollCount() , + _screenWindow->scrollRegion() ); + _screenWindow->resetScrollCount(); + } if (!_image) { // Create _image. @@ -2778,6 +2790,8 @@ void TerminalDisplay::calcGeometry() void TerminalDisplay::makeImage() { + _wallpaper->load(); + calcGeometry(); // confirm that array will be of non-zero size, since the painting code diff --git a/src/TerminalDisplay.h b/src/TerminalDisplay.h index b77b07ac4..72739d513 100644 --- a/src/TerminalDisplay.h +++ b/src/TerminalDisplay.h @@ -31,6 +31,7 @@ #include "Character.h" #include "konsole_export.h" #include "ScreenWindow.h" +#include "ColorScheme.h" class QDrag; class QDragEnterEvent; @@ -90,6 +91,9 @@ public: /** Sets the opacity of the terminal display. */ void setOpacity(qreal opacity); + /** Sets the background picture */ + void setWallpaper(ColorSchemeWallpaper::Ptr p); + /** * This enum describes the location where the scroll bar is positioned in the display widget. */ @@ -791,6 +795,8 @@ private: QRgb _blendColor; + ColorSchemeWallpaper::Ptr _wallpaper; + // list of filters currently applied to the display. used for links and // search highlight TerminalImageFilterChain* _filterChain; diff --git a/src/ViewManager.cpp b/src/ViewManager.cpp index dd028cb78..649f35916 100644 --- a/src/ViewManager.cpp +++ b/src/ViewManager.cpp @@ -852,6 +852,7 @@ void ViewManager::applyProfile(TerminalDisplay* view , const Profile::Ptr info, colorScheme->getColorTable(table , view->randomSeed() ); view->setColorTable(table); view->setOpacity(colorScheme->opacity()); + view->setWallpaper(colorScheme->wallpaper()); // load font view->setAntialias(info->property(Profile::AntiAliasFonts));