From 794e57b9a44f421fa84ce7fbd1b23e5e0c74ee1c Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Sat, 12 May 2007 15:35:39 +0000 Subject: [PATCH] Make use of window transparency setting in the color scheme when compositing support is available. svn path=/trunk/KDE/kdebase/apps/konsole/; revision=663884 --- src/Application.cpp | 5 +++-- src/Application.h | 2 +- src/TerminalDisplay.cpp | 47 +++++++++++++++++++++----------------- src/TerminalDisplay.h | 6 ++++- src/ViewManager.cpp | 1 + src/start.cpp | 50 +++++++++++++++++++++++++++++++++++++++-- 6 files changed, 84 insertions(+), 27 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index 7cabf9b70..1182992fc 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -40,8 +40,9 @@ using namespace Konsole; -Application::Application() - : _sessionList(0) +Application::Application(Display* display , Qt::HANDLE visual, Qt::HANDLE colormap) + : KUniqueApplication(display,visual,colormap) + , _sessionList(0) , _backgroundInstance(0) { // create session manager diff --git a/src/Application.h b/src/Application.h index 213f66043..11bcc5bf6 100644 --- a/src/Application.h +++ b/src/Application.h @@ -51,7 +51,7 @@ Q_OBJECT public: /** Constructs a new Konsole application. */ - Application(); + Application(Display* display , Qt::HANDLE visual, Qt::HANDLE colormap); virtual ~Application(); diff --git a/src/TerminalDisplay.cpp b/src/TerminalDisplay.cpp index 7410187ee..07515e7f6 100644 --- a/src/TerminalDisplay.cpp +++ b/src/TerminalDisplay.cpp @@ -434,7 +434,6 @@ TerminalDisplay::TerminalDisplay(QWidget *parent) //repainting. //the widget may then need to repaint over some of the area in a different colour //but because of the double buffering there won't be any flicker - setAutoFillBackground(true); _gridLayout = new QGridLayout(this); _gridLayout->setMargin(0); @@ -683,6 +682,29 @@ QColor TerminalDisplay::keyboardCursorColor() const return _cursorColor; } +void TerminalDisplay::setOpacity(qreal opacity) +{ + QColor color(_blendColor); + color.setAlphaF(opacity); + _blendColor = color.rgba(); +} + +void TerminalDisplay::drawBackground(QPainter& painter, const QRect& rect, const QColor& backgroundColor) +{ + if ( HAVE_TRANSPARENCY && qAlpha(_blendColor) < 0xff ) + { + QColor color(backgroundColor); + color.setAlpha(qAlpha(_blendColor)); + + painter.save(); + painter.setCompositionMode(QPainter::CompositionMode_Source); + painter.fillRect(rect, color); + painter.restore(); + } + else + painter.fillRect(rect, backgroundColor); +} + /*! attributed string draw primitive */ @@ -718,26 +740,9 @@ void TerminalDisplay::drawAttrStr(QPainter &paint, const QRect& rect, { if (pm || clear || (_blinking && (attr->rendition & RE_BLINK)) || attr->backgroundColor == CharacterColor(COLOR_SPACE_DEFAULT, _colorsInverted ? DEFAULT_FORE_COLOR : DEFAULT_BACK_COLOR) ) - - // draw background colors with 75% opacity - if ( HAVE_TRANSPARENCY && qAlpha(_blendColor) < 0xff ) { - QRgb col = bColor.rgb(); - - quint8 salpha = 192; - quint8 dalpha = 255 - salpha; - - int a, r, g, b; - a = qMin( (qAlpha (col) * salpha) / 255 + (qAlpha (_blendColor) * dalpha) / 255, 255 ); - r = qMin( (qRed (col) * salpha) / 255 + (qRed (_blendColor) * dalpha) / 255, 255 ); - g = qMin( (qGreen (col) * salpha) / 255 + (qGreen (_blendColor) * dalpha) / 255, 255 ); - b = qMin( (qBlue (col) * salpha) / 255 + (qBlue (_blendColor) * dalpha) / 255, 255 ); - - col = a << 24 | r << 16 | g << 8 | b; - //int pixel = a << 24 | (r * a / 255) << 16 | (g * a / 255) << 8 | (b * a / 255); - - paint.fillRect(rect, QColor(col)); - } else - paint.fillRect(rect, bColor); + { + drawBackground(paint,rect,bColor); + } } QString tmpStr = str.simplified(); diff --git a/src/TerminalDisplay.h b/src/TerminalDisplay.h index 51e8c10f1..7ce000b8d 100644 --- a/src/TerminalDisplay.h +++ b/src/TerminalDisplay.h @@ -84,6 +84,8 @@ public: const ColorEntry* colorTable() const; /** Sets the terminal color palette used by the display. */ void setColorTable(const ColorEntry table[]); + /** Sets the opacity of the terminal display. */ + void setOpacity(qreal opacity); /** * This enum describes the location where the scroll bar is positioned in the display widget. @@ -461,7 +463,9 @@ protected: void drawAttrStr(QPainter &paint, const QRect& rect, QString& str, const Character *attr, bool pm, bool clear); - + + void drawBackground(QPainter& painter, const QRect& rect, const QColor& color); + void paintEvent( QPaintEvent * ); void paintContents(QPainter &paint, const QRect &rect); diff --git a/src/ViewManager.cpp b/src/ViewManager.cpp index 74b7d78b0..50d3b767c 100644 --- a/src/ViewManager.cpp +++ b/src/ViewManager.cpp @@ -553,6 +553,7 @@ void ViewManager::applyProfile(TerminalDisplay* view , const QString& profileKey // load colour scheme view->setColorTable(colorScheme->colorTable()); + view->setOpacity(colorScheme->opacity()); // load font view->setVTFont(info->font()); diff --git a/src/start.cpp b/src/start.cpp index 79ad868fe..160c0f2d7 100644 --- a/src/start.cpp +++ b/src/start.cpp @@ -17,12 +17,17 @@ 02110-1301 USA. */ +// Own +#include "Application.h" + +// X11 +#include + // KDE #include #include #include -#include "Application.h" // bump the version to 2.0 before the KDE 4 release #define KONSOLE_VERSION "1.9" @@ -32,6 +37,7 @@ using namespace Konsole; // fills the KAboutData structure with information about contributors to // Konsole void fillAboutData(KAboutData& aboutData); +void getDisplayInformation(Display*& display , Visual*& visual , Colormap& colormap); static KCmdLineOptions options[] = { @@ -68,7 +74,12 @@ extern "C" int KDE_EXPORT kdemain(int argc,char** argv) exit(0); } - Application app; + Display* display = 0; + Visual* visual = 0; + Colormap colormap = 0; + getDisplayInformation(display,visual,colormap); + + Application app(display,Qt::HANDLE(visual),Qt::HANDLE(colormap)); return app.exec(); } @@ -136,3 +147,38 @@ void fillAboutData(KAboutData& aboutData) aboutData.addCredit("",I18N_NOOP("Thanks to many others.\n")); } + +void getDisplayInformation(Display*& display , Visual*& visual , Colormap& colormap) +{ + display = XOpenDisplay(0); // open default display + if (!display) { + qWarning("Cannot connect to the X server"); + exit(1); + } + + int screen = DefaultScreen(display); + int eventBase, errorBase; + + if (XRenderQueryExtension(display, &eventBase, &errorBase)) { + int nvi; + XVisualInfo templ; + templ.screen = screen; + templ.depth = 32; + templ.c_class = TrueColor; + XVisualInfo *xvi = XGetVisualInfo(display, VisualScreenMask | + VisualDepthMask | + VisualClassMask, &templ, &nvi); + + for (int i = 0; i < nvi; ++i) { + XRenderPictFormat* format = XRenderFindVisualFormat(display, + xvi[i].visual); + if (format->type == PictTypeDirect && format->direct.alphaMask) { + visual = xvi[i].visual; + colormap = XCreateColormap(display, RootWindow(display, screen), + visual, AllocNone); + // found ARGB visual + break; + } + } + } +}