2002-01-16 12:26:03 +00:00
|
|
|
/*
|
|
|
|
This is the new kwindecoration kcontrol module
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
Copyright (c) 2004, Sandro Giessl
|
2002-01-16 12:26:03 +00:00
|
|
|
Copyright (c) 2001
|
|
|
|
Karol Szwed <gallium@kde.org>
|
|
|
|
http://gallium.n3.net/
|
|
|
|
|
|
|
|
Supports new kwin configuration plugins, and titlebar button position
|
|
|
|
modification via dnd interface.
|
|
|
|
|
2003-09-16 19:34:03 +00:00
|
|
|
Based on original "kwintheme" (Window Borders)
|
2002-01-16 12:26:03 +00:00
|
|
|
Copyright (C) 2001 Rik Hemsley (rikkus) <rik@kde.org>
|
2003-09-19 11:14:41 +00:00
|
|
|
|
|
|
|
This program 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 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
2002-01-16 12:26:03 +00:00
|
|
|
*/
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
#include <qheader.h>
|
2002-01-16 12:26:03 +00:00
|
|
|
#include <qpainter.h>
|
2004-12-03 22:23:20 +00:00
|
|
|
#include <qlabel.h>
|
|
|
|
#include <qlayout.h>
|
2004-12-09 21:33:16 +00:00
|
|
|
#include <qstyle.h>
|
|
|
|
|
|
|
|
#include <kdebug.h>
|
2004-12-03 22:23:20 +00:00
|
|
|
|
|
|
|
#include <kdialog.h>
|
2002-01-16 12:26:03 +00:00
|
|
|
#include <klocale.h>
|
2003-07-19 08:02:40 +00:00
|
|
|
#include <kglobalsettings.h>
|
2002-01-16 12:26:03 +00:00
|
|
|
#include "buttons.h"
|
|
|
|
#include "pixmaps.h"
|
|
|
|
|
|
|
|
|
2004-12-03 20:31:03 +00:00
|
|
|
#define BUTTONDRAGMIMETYPE "application/x-kde_kwindecoration_buttons"
|
2004-12-09 21:33:16 +00:00
|
|
|
ButtonDrag::ButtonDrag( Button btn, QWidget* parent, const char* name)
|
2004-12-03 20:31:03 +00:00
|
|
|
: QStoredDrag( BUTTONDRAGMIMETYPE, parent, name)
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
QByteArray data;
|
|
|
|
QDataStream stream(data, IO_WriteOnly);
|
|
|
|
stream << btn.name;
|
|
|
|
stream << btn.icon;
|
|
|
|
stream << btn.type.unicode();
|
|
|
|
stream << (int) btn.duplicate;
|
|
|
|
stream << (int) btn.supported;
|
|
|
|
setEncodedData( data );
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
bool ButtonDrag::canDecode( QDropEvent* e )
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-03 20:31:03 +00:00
|
|
|
return e->provides( BUTTONDRAGMIMETYPE );
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
bool ButtonDrag::decode( QDropEvent* e, Button& btn )
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
QByteArray data = e->data( BUTTONDRAGMIMETYPE );
|
|
|
|
if ( data.size() )
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
|
|
|
e->accept();
|
2004-12-09 21:33:16 +00:00
|
|
|
QDataStream stream(data, IO_ReadOnly);
|
|
|
|
stream >> btn.name;
|
|
|
|
stream >> btn.icon;
|
|
|
|
ushort type;
|
|
|
|
stream >> type;
|
|
|
|
btn.type = QChar(type);
|
2004-12-10 15:36:46 +00:00
|
|
|
int duplicate;
|
|
|
|
stream >> duplicate;
|
|
|
|
btn.duplicate = duplicate;
|
|
|
|
int supported;
|
|
|
|
stream >> supported;
|
|
|
|
btn.supported = supported;
|
2002-01-16 12:26:03 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
Button::Button()
|
|
|
|
{
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
Button::Button(const QString& n, const QPixmap& i, QChar t, bool d, bool s)
|
|
|
|
: name(n),
|
|
|
|
icon(i),
|
|
|
|
type(t),
|
|
|
|
duplicate(d),
|
|
|
|
supported(s)
|
|
|
|
{
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
Button::~Button()
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
ButtonSource::ButtonSource(QWidget *parent, const char* name)
|
|
|
|
: KListView(parent, name)
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
setResizeMode(QListView::AllColumns);
|
|
|
|
setDragEnabled(true);
|
|
|
|
setAcceptDrops(true);
|
|
|
|
setDropVisualizer(false);
|
|
|
|
setSorting(-1);
|
|
|
|
header()->setClickEnabled(false);
|
|
|
|
header()->hide();
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
addColumn(i18n("Buttons") );
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
ButtonSource::~ButtonSource()
|
|
|
|
{
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
QSize ButtonSource::sizeHint() const
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
// make the sizeHint height a bit smaller than the one of QListView...
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
if ( cachedSizeHint().isValid() )
|
|
|
|
return cachedSizeHint();
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
constPolish();
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
QSize s( header()->sizeHint() );
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
if ( verticalScrollBar()->isVisible() )
|
|
|
|
s.setWidth( s.width() + style().pixelMetric(QStyle::PM_ScrollBarExtent) );
|
|
|
|
s += QSize(frameWidth()*2,frameWidth()*2);
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
// size hint: 4 lines of text...
|
|
|
|
s.setHeight( s.height() + fontMetrics().lineSpacing()*3 );
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
setCachedSizeHint( s );
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
|
|
|
void ButtonSource::hideAllButtons()
|
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
QListViewItemIterator it(this);
|
|
|
|
while (it.current() ) {
|
|
|
|
it.current()->setVisible(false);
|
|
|
|
++it;
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonSource::showAllButtons()
|
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
QListViewItemIterator it(this);
|
|
|
|
while (it.current() ) {
|
|
|
|
it.current()->setVisible(true);
|
|
|
|
++it;
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
void ButtonSource::showButton( QChar btn )
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
QListViewItemIterator it(this);
|
|
|
|
while (it.current() ) {
|
|
|
|
ButtonSourceItem *item = dynamic_cast<ButtonSourceItem*>(it.current() );
|
|
|
|
if (item && item->button().type == btn) {
|
|
|
|
it.current()->setVisible(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++it;
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
2004-12-09 21:33:16 +00:00
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
void ButtonSource::hideButton( QChar btn )
|
|
|
|
{
|
|
|
|
QListViewItemIterator it(this);
|
|
|
|
while (it.current() ) {
|
|
|
|
ButtonSourceItem *item = dynamic_cast<ButtonSourceItem*>(it.current() );
|
|
|
|
if (item && item->button().type == btn && !item->button().duplicate) {
|
|
|
|
it.current()->setVisible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++it;
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
bool ButtonSource::acceptDrag(QDropEvent* e) const
|
|
|
|
{
|
|
|
|
return acceptDrops() && ButtonDrag::canDecode(e);
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
QDragObject *ButtonSource::dragObject()
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
ButtonSourceItem *i = dynamic_cast<ButtonSourceItem*>(selectedItem() );
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
if (i) {
|
|
|
|
ButtonDrag *bd = new ButtonDrag(i->button(), viewport(), "button_drag");
|
|
|
|
bd->setPixmap(i->button().icon);
|
|
|
|
return bd;
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
ButtonDropSiteItem::ButtonDropSiteItem(const Button& btn)
|
|
|
|
: m_button(btn)
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
ButtonDropSiteItem::~ButtonDropSiteItem()
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
Button ButtonDropSiteItem::button()
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
return m_button;
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
int ButtonDropSiteItem::width()
|
|
|
|
{
|
|
|
|
return m_button.icon.width();
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
int ButtonDropSiteItem::height()
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
return m_button.icon.height();
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
void ButtonDropSiteItem::draw(QPainter *p, QRect r)
|
|
|
|
{
|
|
|
|
p->drawPixmap(r.left(), r.top(), m_button.icon);
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
ButtonDropSite::ButtonDropSite( QWidget* parent, const char* name )
|
2004-12-09 21:33:16 +00:00
|
|
|
: QFrame( parent, name ),
|
|
|
|
m_selected(0)
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
|
|
|
setAcceptDrops( TRUE );
|
|
|
|
setFrameShape( WinPanel );
|
|
|
|
setFrameShadow( Raised );
|
|
|
|
setMinimumHeight( 26 );
|
|
|
|
setMaximumHeight( 26 );
|
|
|
|
setMinimumWidth( 250 ); // Ensure buttons will fit
|
|
|
|
}
|
|
|
|
|
|
|
|
ButtonDropSite::~ButtonDropSite()
|
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
clearLeft();
|
|
|
|
clearRight();
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
void ButtonDropSite::clearLeft()
|
|
|
|
{
|
|
|
|
while (!buttonsLeft.isEmpty() ) {
|
|
|
|
ButtonDropSiteItem *item = buttonsLeft.first();
|
|
|
|
if (removeButton(item) ) {
|
|
|
|
emit buttonRemoved(item->button().type);
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
void ButtonDropSite::clearRight()
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
while (!buttonsRight.isEmpty() ) {
|
|
|
|
ButtonDropSiteItem *item = buttonsRight.first();
|
|
|
|
if (removeButton(item) ) {
|
|
|
|
emit buttonRemoved(item->button().type);
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
void ButtonDropSite::dragMoveEvent( QDragMoveEvent* e )
|
|
|
|
{
|
|
|
|
QPoint p = e->pos();
|
2004-12-09 22:28:55 +00:00
|
|
|
if (leftDropArea().contains(p) || rightDropArea().contains(p) || buttonAt(p) ) {
|
2004-12-09 21:33:16 +00:00
|
|
|
e->accept();
|
2004-12-09 22:28:55 +00:00
|
|
|
|
|
|
|
// 2 pixel wide drop visualizer...
|
|
|
|
QRect r = contentsRect();
|
|
|
|
int x = -1;
|
|
|
|
if (leftDropArea().contains(p) ) {
|
|
|
|
x = leftDropArea().left();
|
|
|
|
} else if (rightDropArea().contains(p) ) {
|
|
|
|
x = rightDropArea().right()+1;
|
|
|
|
} else {
|
|
|
|
ButtonDropSiteItem *item = buttonAt(p);
|
|
|
|
if (item) {
|
|
|
|
if (p.x() < item->rect.left()+item->rect.width()/2 ) {
|
|
|
|
x = item->rect.left();
|
|
|
|
} else {
|
|
|
|
x = item->rect.right()+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (x != -1) {
|
|
|
|
QRect tmpRect(x, r.y(), 2, r.height() );
|
|
|
|
if (tmpRect != m_oldDropVisualizer) {
|
|
|
|
cleanDropVisualizer();
|
|
|
|
m_oldDropVisualizer = tmpRect;
|
|
|
|
update(tmpRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2004-12-09 21:33:16 +00:00
|
|
|
e->ignore();
|
2004-12-09 22:28:55 +00:00
|
|
|
|
|
|
|
cleanDropVisualizer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonDropSite::cleanDropVisualizer()
|
|
|
|
{
|
|
|
|
if (m_oldDropVisualizer.isValid())
|
|
|
|
{
|
|
|
|
QRect rect = m_oldDropVisualizer;
|
|
|
|
m_oldDropVisualizer = QRect(); // rect is invalid
|
|
|
|
update(rect);
|
|
|
|
}
|
2004-12-09 21:33:16 +00:00
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
|
|
|
void ButtonDropSite::dragEnterEvent( QDragEnterEvent* e )
|
|
|
|
{
|
|
|
|
if ( ButtonDrag::canDecode( e ) )
|
|
|
|
e->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonDropSite::dragLeaveEvent( QDragLeaveEvent* /* e */ )
|
|
|
|
{
|
2004-12-09 22:28:55 +00:00
|
|
|
cleanDropVisualizer();
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonDropSite::dropEvent( QDropEvent* e )
|
|
|
|
{
|
2004-12-09 22:28:55 +00:00
|
|
|
cleanDropVisualizer();
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
QPoint p = e->pos();
|
|
|
|
|
|
|
|
// collect information where to insert the dropped button
|
|
|
|
ButtonList *buttonList = 0;
|
|
|
|
ButtonList::iterator buttonPosition;
|
|
|
|
|
|
|
|
if (leftDropArea().contains(p) ) {
|
|
|
|
buttonList = &buttonsLeft;
|
|
|
|
buttonPosition = buttonsLeft.end();
|
|
|
|
} else if (rightDropArea().contains(p) ) {
|
|
|
|
buttonList = &buttonsRight;
|
|
|
|
buttonPosition = buttonsRight.begin();
|
|
|
|
} else {
|
|
|
|
ButtonDropSiteItem *aboveItem = buttonAt(p);
|
|
|
|
if (!aboveItem)
|
|
|
|
return; // invalid drop. hasn't occured _over_ a button (or left/right dropArea), return...
|
|
|
|
|
|
|
|
ButtonList::iterator it;
|
|
|
|
if (!getItemIterator(aboveItem, buttonList, it) ) {
|
|
|
|
// didn't find the aboveItem. unlikely to happen since buttonAt() already seems to have found
|
|
|
|
// something valid. anyway...
|
|
|
|
return;
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
// got the list and the aboveItem position. now determine if the item should be inserted
|
|
|
|
// before aboveItem or after aboveItem.
|
|
|
|
QRect aboveItemRect = aboveItem->rect;
|
|
|
|
if (!aboveItemRect.isValid() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (p.x() < aboveItemRect.left()+aboveItemRect.width()/2 ) {
|
|
|
|
// insert before the item
|
|
|
|
buttonPosition = it;
|
|
|
|
} else {
|
|
|
|
if (it != buttonList->end() )
|
|
|
|
buttonPosition = ++it;
|
2002-01-16 12:26:03 +00:00
|
|
|
else
|
2004-12-09 21:33:16 +00:00
|
|
|
buttonPosition = it; // already at the end(), can't increment the iterator!
|
|
|
|
}
|
|
|
|
}
|
2003-09-16 19:34:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
// know where to insert the button. now see if we can use an existing item (drag within the widget = move)
|
|
|
|
// orneed to create a new one
|
|
|
|
ButtonDropSiteItem *buttonItem = 0;
|
|
|
|
if (e->source() == this && m_selected) {
|
|
|
|
ButtonList *oldList = 0;
|
|
|
|
ButtonList::iterator oldPos;
|
|
|
|
if (getItemIterator(m_selected, oldList, oldPos) ) {
|
|
|
|
if (oldPos == buttonPosition)
|
|
|
|
return; // button didn't change its position during the drag...
|
|
|
|
|
|
|
|
oldList->remove(oldPos);
|
|
|
|
buttonItem = m_selected;
|
|
|
|
} else {
|
|
|
|
return; // m_selected not found, return...
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// create new button from the drop object...
|
|
|
|
Button btn;
|
|
|
|
if (ButtonDrag::decode(e, btn) ) {
|
|
|
|
buttonItem = new ButtonDropSiteItem(btn);
|
|
|
|
} else {
|
|
|
|
return; // something has gone wrong while we were trying to decode the drop event
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
// now the item can actually be inserted into the list! :)
|
|
|
|
(*buttonList).insert(buttonPosition, buttonItem);
|
|
|
|
emit buttonAdded(buttonItem->button().type);
|
|
|
|
emit changed();
|
|
|
|
recalcItemGeometry();
|
|
|
|
update();
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
bool ButtonDropSite::getItemIterator(ButtonDropSiteItem *item, ButtonList* &list, ButtonList::iterator &iterator)
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
if (!item)
|
|
|
|
return false;
|
2003-09-16 19:34:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
ButtonList::iterator it = buttonsLeft.find(item); // try the left list first...
|
|
|
|
if (it == buttonsLeft.end() ) {
|
|
|
|
it = buttonsRight.find(item); // try the right list...
|
|
|
|
if (it == buttonsRight.end() ) {
|
|
|
|
return false; // item hasn't been found in one of the list, return...
|
|
|
|
} else {
|
|
|
|
list = &buttonsRight;
|
|
|
|
iterator = it;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
list = &buttonsLeft;
|
|
|
|
iterator = it;
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
QRect ButtonDropSite::leftDropArea()
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
// return a 10 pixel drop area...
|
|
|
|
QRect r = contentsRect();
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
int leftButtonsWidth = calcButtonListWidth(buttonsLeft);
|
|
|
|
return QRect(r.left()+leftButtonsWidth, r.top(), 10, r.height() );
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
QRect ButtonDropSite::rightDropArea()
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
// return a 10 pixel drop area...
|
|
|
|
QRect r = contentsRect();
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
int rightButtonsWidth = calcButtonListWidth(buttonsRight);
|
|
|
|
return QRect(r.right()-rightButtonsWidth-10, r.top(), 10, r.height() );
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
void ButtonDropSite::mousePressEvent( QMouseEvent* e )
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
// TODO: only start the real drag after some drag distance
|
|
|
|
m_selected = buttonAt(e->pos() );
|
|
|
|
if (m_selected) {
|
|
|
|
ButtonDrag *bd = new ButtonDrag(m_selected->button(), this);
|
|
|
|
bd->setPixmap(m_selected->button().icon);
|
|
|
|
bd->dragMove();
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
void ButtonDropSite::resizeEvent(QResizeEvent*)
|
|
|
|
{
|
|
|
|
recalcItemGeometry();
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
void ButtonDropSite::recalcItemGeometry()
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
QRect r = contentsRect();
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
// update the geometry of the items in the left button list
|
|
|
|
int offset = r.left();
|
|
|
|
for (ButtonList::const_iterator it = buttonsLeft.begin(); it != buttonsLeft.end(); ++it) {
|
|
|
|
int w = (*it)->width();
|
|
|
|
(*it)->rect = QRect(offset, r.top(), w, (*it)->height() );
|
|
|
|
offset += w;
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
// the right button list...
|
|
|
|
offset = r.right() - calcButtonListWidth(buttonsRight);
|
|
|
|
for (ButtonList::const_iterator it = buttonsRight.begin(); it != buttonsRight.end(); ++it) {
|
|
|
|
int w = (*it)->width();
|
|
|
|
(*it)->rect = QRect(offset, r.top(), w, (*it)->height() );
|
|
|
|
offset += w;
|
|
|
|
}
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
ButtonDropSiteItem *ButtonDropSite::buttonAt(QPoint p) {
|
|
|
|
// try to find the item in the left button list
|
|
|
|
for (ButtonList::const_iterator it = buttonsLeft.begin(); it != buttonsLeft.end(); ++it) {
|
|
|
|
if ( (*it)->rect.contains(p) ) {
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
// try to find the item in the right button list
|
|
|
|
for (ButtonList::const_iterator it = buttonsRight.begin(); it != buttonsRight.end(); ++it) {
|
|
|
|
if ( (*it)->rect.contains(p) ) {
|
|
|
|
return *it;
|
2003-09-16 19:34:03 +00:00
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
bool ButtonDropSite::removeButton(ButtonDropSiteItem *item) {
|
|
|
|
if (!item)
|
|
|
|
return false;
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
// try to remove the item from the left button list
|
|
|
|
if (buttonsLeft.remove(item) >= 1) {
|
|
|
|
return true;
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
// try to remove the item from the right button list
|
|
|
|
if (buttonsRight.remove(item) >= 1) {
|
|
|
|
return true;
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
int ButtonDropSite::calcButtonListWidth(const ButtonList& btns)
|
|
|
|
{
|
|
|
|
int w = 0;
|
|
|
|
for (ButtonList::const_iterator it = btns.begin(); it != btns.end(); ++it) {
|
|
|
|
w += (*it)->width();
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ButtonDropSite::removeSelectedButton()
|
|
|
|
{
|
|
|
|
bool succ = removeButton(m_selected);
|
|
|
|
if (succ) {
|
|
|
|
emit buttonRemoved(m_selected->button().type);
|
|
|
|
emit changed();
|
|
|
|
delete m_selected;
|
|
|
|
m_selected = 0;
|
|
|
|
recalcItemGeometry();
|
|
|
|
update(); // repaint...
|
2003-09-16 19:34:03 +00:00
|
|
|
}
|
2002-01-16 12:26:03 +00:00
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
return succ;
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
void ButtonDropSite::drawButtonList(QPainter *p, const ButtonList& btns, int offset)
|
2002-01-16 12:26:03 +00:00
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
for (ButtonList::const_iterator it = btns.begin(); it != btns.end(); ++it) {
|
|
|
|
QRect itemRect = (*it)->rect;
|
|
|
|
if (itemRect.isValid() ) {
|
|
|
|
(*it)->draw(p, itemRect);
|
|
|
|
}
|
|
|
|
offset += (*it)->width();
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonDropSite::drawContents( QPainter* p )
|
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
int leftoffset = calcButtonListWidth( buttonsLeft );
|
|
|
|
int rightoffset = calcButtonListWidth( buttonsRight );
|
2002-01-16 12:26:03 +00:00
|
|
|
int offset = 3;
|
|
|
|
|
|
|
|
QRect r = contentsRect();
|
|
|
|
|
|
|
|
// Shrink by 1
|
|
|
|
r.moveBy(1 + leftoffset, 1);
|
|
|
|
r.setWidth( r.width() - 2 - leftoffset - rightoffset );
|
|
|
|
r.setHeight( r.height() - 2 );
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
drawButtonList( p, buttonsLeft, offset );
|
2002-01-16 12:26:03 +00:00
|
|
|
|
|
|
|
QColor c1( 0x0A, 0x5F, 0x89 ); // KDE 2 titlebar default colour
|
|
|
|
p->fillRect( r, c1 );
|
|
|
|
p->setPen( Qt::white );
|
2003-07-19 08:02:40 +00:00
|
|
|
p->setFont( QFont( KGlobalSettings::generalFont().family(), 12, QFont::Bold) );
|
2002-01-16 12:26:03 +00:00
|
|
|
p->drawText( r, AlignLeft | AlignVCenter, i18n("KDE") );
|
|
|
|
|
|
|
|
offset = geometry().width() - 3 - rightoffset;
|
2004-12-09 21:33:16 +00:00
|
|
|
drawButtonList( p, buttonsRight, offset );
|
2004-12-09 22:28:55 +00:00
|
|
|
|
|
|
|
if (m_oldDropVisualizer.isValid() )
|
|
|
|
{
|
|
|
|
p->fillRect(m_oldDropVisualizer, Dense4Pattern);
|
|
|
|
}
|
2004-12-09 21:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ButtonSourceItem::ButtonSourceItem(QListView * parent, const Button& btn)
|
|
|
|
: QListViewItem(parent),
|
|
|
|
m_button(btn)
|
|
|
|
{
|
|
|
|
setButton(btn);
|
|
|
|
}
|
|
|
|
|
|
|
|
ButtonSourceItem::~ButtonSourceItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonSourceItem::paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int align)
|
|
|
|
{
|
|
|
|
if (m_button.supported) {
|
|
|
|
QListViewItem::paintCell(p,cg,column,width,align);
|
|
|
|
} else {
|
|
|
|
// grey out unsupported buttons
|
|
|
|
QColorGroup cg2 = cg;
|
|
|
|
cg2.setColor(QColorGroup::Text, cg.mid() );
|
|
|
|
QListViewItem::paintCell(p,cg2,column,width,align);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonSourceItem::setButton(const Button& btn)
|
|
|
|
{
|
|
|
|
m_button = btn;
|
|
|
|
if (btn.supported) {
|
|
|
|
setPixmap(0, btn.icon);
|
|
|
|
setText(0, btn.name);
|
|
|
|
} else {
|
|
|
|
setPixmap(0, btn.icon);
|
|
|
|
setText(0, i18n("%1 (unavailable)").arg(btn.name) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Button ButtonSourceItem::button() const
|
|
|
|
{
|
|
|
|
return m_button;
|
2002-01-16 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
|
2004-12-03 22:23:20 +00:00
|
|
|
ButtonPositionWidget::ButtonPositionWidget(QWidget *parent, const char* name)
|
|
|
|
: QWidget(parent,name)
|
|
|
|
{
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout(this, 0, KDialog::spacingHint() );
|
2004-12-09 21:33:16 +00:00
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
|
2004-12-03 22:23:20 +00:00
|
|
|
|
|
|
|
QLabel* label = new QLabel( this );
|
2004-12-09 21:33:16 +00:00
|
|
|
m_dropSite = new ButtonDropSite( this );
|
2004-12-03 22:23:20 +00:00
|
|
|
label->setAlignment( int( QLabel::WordBreak ) );
|
|
|
|
label->setText( i18n( "To add or remove titlebar buttons, simply <i>drag</i> items "
|
|
|
|
"between the available item list and the titlebar preview. Similarly, "
|
|
|
|
"drag items within the titlebar preview to re-position them.") );
|
2004-12-09 21:33:16 +00:00
|
|
|
m_buttonSource = new ButtonSource(this, "button_source");
|
2004-12-03 22:23:20 +00:00
|
|
|
|
|
|
|
layout->addWidget(label);
|
2004-12-09 21:33:16 +00:00
|
|
|
layout->addWidget(m_dropSite);
|
|
|
|
layout->addWidget(m_buttonSource);
|
|
|
|
|
|
|
|
connect( m_dropSite, SIGNAL(buttonAdded(QChar)), m_buttonSource, SLOT(hideButton(QChar)) );
|
|
|
|
connect( m_dropSite, SIGNAL(buttonRemoved(QChar)), m_buttonSource, SLOT(showButton(QChar)) );
|
|
|
|
connect( m_buttonSource, SIGNAL(dropped(QDropEvent*, QListViewItem*)), m_dropSite, SLOT(removeSelectedButton()) );
|
|
|
|
|
|
|
|
connect( m_dropSite, SIGNAL(changed()), SIGNAL(changed()) );
|
|
|
|
|
|
|
|
// insert all possible buttons into the source (backwards to keep the preferred order...)
|
|
|
|
bool dummy;
|
|
|
|
new ButtonSourceItem(m_buttonSource, getButton('R', dummy) );
|
|
|
|
new ButtonSourceItem(m_buttonSource, getButton('L', dummy) );
|
|
|
|
new ButtonSourceItem(m_buttonSource, getButton('B', dummy) );
|
|
|
|
new ButtonSourceItem(m_buttonSource, getButton('F', dummy) );
|
|
|
|
new ButtonSourceItem(m_buttonSource, getButton('X', dummy) );
|
|
|
|
new ButtonSourceItem(m_buttonSource, getButton('A', dummy) );
|
|
|
|
new ButtonSourceItem(m_buttonSource, getButton('I', dummy) );
|
|
|
|
new ButtonSourceItem(m_buttonSource, getButton('H', dummy) );
|
|
|
|
new ButtonSourceItem(m_buttonSource, getButton('S', dummy) );
|
|
|
|
new ButtonSourceItem(m_buttonSource, getButton('M', dummy) );
|
|
|
|
new ButtonSourceItem(m_buttonSource, getButton('_', dummy) );
|
2004-12-03 22:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ButtonPositionWidget::~ButtonPositionWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-12-09 21:33:16 +00:00
|
|
|
Button ButtonPositionWidget::getButton(QChar type, bool& success) {
|
|
|
|
success = true;
|
|
|
|
|
|
|
|
if (type == 'R') {
|
|
|
|
return Button(i18n("Resize"), QPixmap(button_resize_xpm), 'R', false, true);
|
|
|
|
} else if (type == 'L') {
|
|
|
|
return Button(i18n("Shade"), QPixmap(button_shade_xpm), 'L', false, true);
|
|
|
|
} else if (type == 'B') {
|
|
|
|
return Button(i18n("Keep Below Others"), QPixmap(button_below_others_xpm), 'B', false, true);
|
|
|
|
} else if (type == 'F') {
|
|
|
|
return Button(i18n("Keep Above Others"), QPixmap(button_above_others_xpm), 'F', false, true);
|
|
|
|
} else if (type == 'X') {
|
|
|
|
return Button(i18n("Close"), QPixmap(button_close_xpm), 'X', false, true);
|
|
|
|
} else if (type == 'A') {
|
|
|
|
return Button(i18n("Maximize"), QPixmap(button_maximize_xpm), 'A', false, true);
|
|
|
|
} else if (type == 'I') {
|
|
|
|
return Button(i18n("Minimize"), QPixmap(button_minimize_xpm), 'I', false, true);
|
|
|
|
} else if (type == 'H') {
|
|
|
|
return Button(i18n("Help"), QPixmap(button_help_xpm), 'H', false, true);
|
|
|
|
} else if (type == 'S') {
|
|
|
|
return Button(i18n("On All Desktops"), QPixmap(button_on_all_desktops_xpm), 'S', false, true);
|
|
|
|
} else if (type == 'M') {
|
|
|
|
return Button(i18n("Menu"), QPixmap(button_menu_xpm), 'M', false, true);
|
|
|
|
} else if (type == '_') {
|
|
|
|
return Button(i18n("--- spacer ---"), QPixmap(button_spacer_xpm), '_', true, true);
|
|
|
|
} else {
|
|
|
|
return Button();
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-03 22:23:20 +00:00
|
|
|
QString ButtonPositionWidget::buttonsLeft() const
|
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
ButtonList btns = m_dropSite->buttonsLeft;
|
|
|
|
QString btnString = "";
|
|
|
|
for (ButtonList::const_iterator it = btns.begin(); it != btns.end(); ++it) {
|
|
|
|
btnString.append( (*it)->button().type );
|
|
|
|
}
|
|
|
|
return btnString;
|
2004-12-03 22:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ButtonPositionWidget::buttonsRight() const
|
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
ButtonList btns = m_dropSite->buttonsRight;
|
|
|
|
QString btnString = "";
|
|
|
|
for (ButtonList::const_iterator it = btns.begin(); it != btns.end(); ++it) {
|
|
|
|
btnString.append( (*it)->button().type );
|
|
|
|
}
|
|
|
|
return btnString;
|
2004-12-03 22:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonPositionWidget::setButtonsLeft(const QString &buttons)
|
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
// to keep the button lists consistent, first remove all left buttons, then add buttons again...
|
|
|
|
m_dropSite->clearLeft();
|
|
|
|
|
|
|
|
for (uint i = 0; i < buttons.length(); ++i) {
|
|
|
|
bool succ = false;
|
|
|
|
Button btn = getButton(buttons[i], succ);
|
|
|
|
if (succ) {
|
|
|
|
m_dropSite->buttonsLeft.append(new ButtonDropSiteItem(btn) );
|
|
|
|
m_buttonSource->hideButton(btn.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_dropSite->recalcItemGeometry();
|
|
|
|
m_dropSite->update();
|
2004-12-03 22:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonPositionWidget::setButtonsRight(const QString &buttons)
|
|
|
|
{
|
2004-12-09 21:33:16 +00:00
|
|
|
// to keep the button lists consistent, first remove all left buttons, then add buttons again...
|
|
|
|
m_dropSite->clearRight();
|
|
|
|
|
|
|
|
for (uint i = 0; i < buttons.length(); ++i) {
|
|
|
|
bool succ = false;
|
|
|
|
Button btn = getButton(buttons[i], succ);
|
|
|
|
if (succ) {
|
|
|
|
m_dropSite->buttonsRight.append(new ButtonDropSiteItem(btn) );
|
|
|
|
m_buttonSource->hideButton(btn.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_dropSite->recalcItemGeometry();
|
|
|
|
m_dropSite->update();
|
2004-12-03 22:23:20 +00:00
|
|
|
}
|
|
|
|
|
2002-01-16 12:26:03 +00:00
|
|
|
#include "buttons.moc"
|
|
|
|
// vim: ts=4
|
2004-12-03 22:23:20 +00:00
|
|
|
// kate: space-indent off; tab-width 4;
|