From 0f3ba435d9038638f805e4431dffc7d26d670915 Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Sun, 20 Nov 2011 22:39:50 +0100 Subject: [PATCH] lalalalala --- src/libtomahawk/utils/tomahawkutilsgui.cpp | 294 +++++++++++++++++++++ src/libtomahawk/utils/tomahawkutilsgui.h | 51 ++++ 2 files changed, 345 insertions(+) create mode 100644 src/libtomahawk/utils/tomahawkutilsgui.cpp create mode 100644 src/libtomahawk/utils/tomahawkutilsgui.h diff --git a/src/libtomahawk/utils/tomahawkutilsgui.cpp b/src/libtomahawk/utils/tomahawkutilsgui.cpp new file mode 100644 index 000000000..a021e87fc --- /dev/null +++ b/src/libtomahawk/utils/tomahawkutilsgui.cpp @@ -0,0 +1,294 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#include "config.h" +#include "tomahawkutilsgui.h" + +#include +#include +#include +#include +#include +#include + +#ifdef Q_WS_X11 + #include + #include +#endif + +#ifdef Q_WS_WIN + #include + #include +#endif + +namespace TomahawkUtils +{ +static int s_headerHeight = 0; + + +QPixmap +createDragPixmap( MediaType type, int itemCount ) +{ + // If more than one item is dragged, align the items inside a + // rectangular grid. The maximum grid size is limited to 5 x 5 items. + int xCount = 3; + int size = 32; + + if ( itemCount > 16 ) + { + xCount = 5; + size = 16; + } else if( itemCount > 9 ) + { + xCount = 4; + size = 22; + } + + if( itemCount < xCount ) + { + xCount = itemCount; + } + + int yCount = itemCount / xCount; + if( itemCount % xCount != 0 ) + { + ++yCount; + } + if( yCount > xCount ) + { + yCount = xCount; + } + // Draw the selected items into the grid cells + QPixmap dragPixmap( xCount * size + xCount - 1, yCount * size + yCount - 1 ); + dragPixmap.fill( Qt::transparent ); + + QPainter painter( &dragPixmap ); + painter.setRenderHint( QPainter::Antialiasing ); + + QPixmap pixmap; + switch ( type ) + { + case MediaTypeArtist: + pixmap = QPixmap( ":/data/images/artist-icon.png" ).scaledToWidth( size, Qt::SmoothTransformation ); + break; + case MediaTypeAlbum: + pixmap = QPixmap( ":/data/images/album-icon.png" ).scaledToWidth( size, Qt::SmoothTransformation ); + break; + case MediaTypeTrack: + pixmap = QPixmap( QString( ":/data/images/track-icon-%2x%2.png" ).arg( size ) ); + break; + } + + int x = 0; + int y = 0; + for( int i = 0; i < itemCount; ++i ) + { + + painter.drawPixmap( x, y, pixmap ); + + x += size + 1; + if ( x >= dragPixmap.width() ) + { + x = 0; + y += size + 1; + } + if ( y >= dragPixmap.height() ) + { + break; + } + } + + return dragPixmap; +} + + +void +drawBackgroundAndNumbers( QPainter* painter, const QString& text, const QRect& figRectIn ) +{ + QRect figRect = figRectIn; + if ( text.length() == 1 ) + figRect.adjust( -painter->fontMetrics().averageCharWidth(), 0, 0, 0 ); + + QPen origpen = painter->pen(); + QPen pen = origpen; + pen.setWidth( 1.0 ); + painter->setPen( pen ); + painter->drawRect( figRect ); + + // circles look bad. make it an oval. (thanks, apple) + const int bulgeWidth = 8; + const int offset = 0; // number of pixels to begin, counting inwards from figRect.x() and figRect.width(). 0 means start at each end, negative means start inside the rect. + + QPainterPath ppath; + ppath.moveTo( QPoint( figRect.x() + offset, figRect.y() + figRect.height() / 2 ) ); + QRect leftArcRect( figRect.x() + offset - bulgeWidth, figRect.y(), 2*bulgeWidth, figRect.height() ); + ppath.arcTo( leftArcRect, 90, 180 ); + painter->drawPath( ppath ); + + ppath = QPainterPath(); + ppath.moveTo( figRect.x() + figRect.width() - offset, figRect.y() + figRect.height() / 2 ); + leftArcRect = QRect( figRect.x() + figRect.width() - offset - bulgeWidth, figRect.y(), 2*bulgeWidth, figRect.height() ); + ppath.arcTo( leftArcRect, 270, 180 ); + painter->drawPath( ppath ); + + painter->setPen( origpen ); + +#ifdef Q_WS_MAC + figRect.adjust( -1, 0, 0, 0 ); +#endif + + QTextOption to( Qt::AlignCenter ); + painter->setPen( Qt::white ); + painter->drawText( figRect.adjusted( -5, 0, 6, 0 ), text, to ); +} + + +void +drawQueryBackground( QPainter* p, const QPalette& palette, const QRect& r, qreal lightnessFactor ) +{ + p->setPen( palette.mid().color().lighter( lightnessFactor * 100 ) ); + p->setBrush( palette.highlight().color().lighter( lightnessFactor * 100 ) ); + p->drawRoundedRect( r, 4.0, 4.0 ); +} + + +void +unmarginLayout( QLayout* layout ) +{ + layout->setContentsMargins( 0, 0, 0, 0 ); + layout->setMargin( 0 ); + layout->setSpacing( 0 ); + + for ( int i = 0; i < layout->count(); i++ ) + { + QLayout* childLayout = layout->itemAt( i )->layout(); + if ( childLayout ) + unmarginLayout( childLayout ); + } +} + + +QWidget* +tomahawkWindow() +{ + QWidgetList widgetList = qApp->topLevelWidgets(); + int i = 0; + while( i < widgetList.count() && widgetList.at( i )->objectName() != "TH_Main_Window" ) + i++; + + if ( i == widgetList.count() ) + { + qDebug() << Q_FUNC_INFO << "could not find main Tomahawk mainwindow"; + Q_ASSERT( false ); + return 0; + } + + QWidget *widget = widgetList.at( i ); + return widget; +} + + +#ifndef Q_WS_MAC +void +bringToFront() +{ +#if defined(Q_WS_X11) + { + qDebug() << Q_FUNC_INFO; + + QWidget* widget = tomahawkWindow(); + if ( !widget ) + return; + + widget->show(); + widget->activateWindow(); + widget->raise(); + + WId wid = widget->winId(); + NETWM::init(); + + XEvent e; + e.xclient.type = ClientMessage; + e.xclient.message_type = NETWM::NET_ACTIVE_WINDOW; + e.xclient.display = QX11Info::display(); + e.xclient.window = wid; + e.xclient.format = 32; + e.xclient.data.l[0] = 2; + e.xclient.data.l[1] = QX11Info::appTime(); + e.xclient.data.l[2] = 0; + e.xclient.data.l[3] = 0l; + e.xclient.data.l[4] = 0l; + + XSendEvent( QX11Info::display(), RootWindow( QX11Info::display(), DefaultScreen( QX11Info::display() ) ), False, SubstructureRedirectMask | SubstructureNotifyMask, &e ); + } +#elif defined(Q_WS_WIN) + { + qDebug() << Q_FUNC_INFO; + + QWidget* widget = tomahawkWindow(); + if ( !widget ) + return; + + widget->show(); + widget->activateWindow(); + widget->raise(); + + WId wid = widget->winId(); + + HWND hwndActiveWin = GetForegroundWindow(); + int idActive = GetWindowThreadProcessId(hwndActiveWin, NULL); + if ( AttachThreadInput(GetCurrentThreadId(), idActive, TRUE) ) + { + SetForegroundWindow( wid ); + SetFocus( wid ); + AttachThreadInput(GetCurrentThreadId(), idActive, FALSE); + } + } +#endif +} +#endif + + +QPixmap +createAvatarFrame( const QPixmap &avatar ) +{ + QPixmap frame( ":/data/images/avatar_frame.png" ); + QPixmap scaledAvatar = avatar.scaled( frame.height() * 75 / 100, frame.width() * 75 / 100, Qt::KeepAspectRatio, Qt::SmoothTransformation ); + + QPainter painter( &frame ); + painter.drawPixmap( (frame.height() - scaledAvatar.height()) / 2, (frame.width() - scaledAvatar.width()) / 2, scaledAvatar ); + + return frame; +} + + +int +headerHeight() +{ + return s_headerHeight; +} + + +void +setHeaderHeight( int height ) +{ + s_headerHeight = height; +} + + +} // ns diff --git a/src/libtomahawk/utils/tomahawkutilsgui.h b/src/libtomahawk/utils/tomahawkutilsgui.h new file mode 100644 index 000000000..6f8f08674 --- /dev/null +++ b/src/libtomahawk/utils/tomahawkutilsgui.h @@ -0,0 +1,51 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#ifndef TOMAHAWKUTILSGUI_H +#define TOMAHAWKUTILSGUI_H + +#include "tomahawkutils.h" +#include "dllmacro.h" + +class QPainter; +class QColor; +class QPixmap; +class QLayout; +class QPalette; +class QRect; + +namespace TomahawkUtils +{ + DLLEXPORT void drawQueryBackground( QPainter* p, const QPalette& palette, const QRect& r, qreal lightnessFactor = 1 ); + DLLEXPORT QWidget* tomahawkWindow(); + /// Platform-specific bringing tomahawk mainwindow to front, b/c qt's activate() and such don't seem to work well enough for us + DLLEXPORT void bringToFront(); + + DLLEXPORT QPixmap createAvatarFrame( const QPixmap &avatar ); + + DLLEXPORT QColor alphaBlend( const QColor& colorFrom, const QColor& colorTo, float opacity ); + DLLEXPORT QPixmap createDragPixmap( MediaType type, int itemCount = 1 ); + + DLLEXPORT void drawBackgroundAndNumbers( QPainter* p, const QString& text, const QRect& rect ); + DLLEXPORT void unmarginLayout( QLayout* layout ); + + DLLEXPORT int headerHeight(); + DLLEXPORT void setHeaderHeight( int height ); +} + +#endif // TOMAHAWKUTILSGUI_H