mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-06 14:16:32 +02:00
lalalalala
This commit is contained in:
294
src/libtomahawk/utils/tomahawkutilsgui.cpp
Normal file
294
src/libtomahawk/utils/tomahawkutilsgui.cpp
Normal file
@@ -0,0 +1,294 @@
|
|||||||
|
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||||
|
*
|
||||||
|
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "tomahawkutilsgui.h"
|
||||||
|
|
||||||
|
#include <QtGui/QLayout>
|
||||||
|
#include <QtGui/QPainter>
|
||||||
|
#include <QtGui/QPixmap>
|
||||||
|
#include <QtGui/QPalette>
|
||||||
|
#include <QtGui/QApplication>
|
||||||
|
#include <QtGui/QWidget>
|
||||||
|
|
||||||
|
#ifdef Q_WS_X11
|
||||||
|
#include <QtGui/QX11Info>
|
||||||
|
#include <libqnetwm/netwm.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef Q_WS_WIN
|
||||||
|
#include <windows.h>
|
||||||
|
#include <windowsx.h>
|
||||||
|
#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
|
51
src/libtomahawk/utils/tomahawkutilsgui.h
Normal file
51
src/libtomahawk/utils/tomahawkutilsgui.h
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||||
|
*
|
||||||
|
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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
|
Reference in New Issue
Block a user