1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
/* This file is part of the KDE project
* Copyright (C) 2010 Casper Boemann <cbo@boemann.dk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "SpecialButton.h"
#include "StylesWidget.h"
#include <QFrame>
#include <QHBoxLayout>
#include <QDesktopWidget>
#include <QPixmap>
#include <QLabel>
#include <QHideEvent>
#include <QShowEvent>
#include <KDebug>
SpecialButton::SpecialButton(QWidget *parent)
: QFrame(parent),
m_stylesWidget(0),
m_preview(0)
{
setFrameShape(QFrame::StyledPanel);
setFrameShadow(QFrame::Sunken);
setMinimumSize(50,32);
setMaximumHeight(25);
m_preview = new QLabel();
m_preview->setAutoFillBackground(true);
m_preview->setBackgroundRole(QPalette::Base);
m_preview->setMinimumWidth(50);
m_preview->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
QHBoxLayout *l = new QHBoxLayout(this);
l->addWidget(m_preview);
l->setMargin(0);
setLayout(l);
isPopupVisible = false;
}
SpecialButton::~SpecialButton()
{
delete m_preview;
}
void SpecialButton::setStylePreview(const QPixmap &pm)
{
m_preview->setPixmap(pm);
}
void SpecialButton::showPopup()
{
if (!m_stylesWidget) {
return;
}
QRect popupRect(mapToGlobal(QPoint(0, height())), m_stylesWidget->sizeHint());
// Make sure the popup is not drawn outside the screen area
QRect screenRect = QApplication::desktop()->availableGeometry(this);
if (popupRect.right() > screenRect.right())
popupRect.translate(screenRect.right() - popupRect.right(), 0);
if (popupRect.left() < screenRect.left())
popupRect.translate(screenRect.left() - popupRect.left(), 0);
if (popupRect.bottom() > screenRect.bottom())
popupRect.translate(0, -(height() + m_stylesWidget->height()));
m_stylesWidget->setGeometry(popupRect);
m_stylesWidget->raise();
m_stylesWidget->show();
isPopupVisible = true;
}
void SpecialButton::hidePopup()
{
m_stylesWidget->hide();
isPopupVisible = false;
}
void SpecialButton::setStylesWidget(StylesWidget *stylesWidget)
{
m_stylesWidget = stylesWidget;
}
void SpecialButton::mousePressEvent(QMouseEvent *)
{
if (!isPopupVisible) {
showPopup();
}
else {
hidePopup();
}
}
#include <SpecialButton.moc>
|