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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/* This file is part of the KDE project
* Copyright (C) 2011 Pierre Stirnweiss <pstirnweiss@googlemail.com>
*
* 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.
*/
/*
* This class is heavily inspired by KLineEdit, so here goes credit because credit is due (from klineedit.h):
* This class was originally inspired by Torben Weis'
* fileentry.cpp for KFM II.
* Sven Radej <sven.radej@iname.com>
* Patrick Ward <PAT_WARD@HP-USA-om5.om.hp.com>
* Preston Brown <pbrown@kde.org>
* Completely re-designed:
* Dawit Alemayehu <adawit@kde.org>
*/
#include "StylesComboPreview.h"
#include <QImage>
#include <QLineEdit>
#include <QModelIndex>
#include <QPainter>
#include <QPaintEvent>
#include <QFocusEvent>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QPushButton>
#include <QString>
#include <KIcon>
#include <KLocale>
#include <KDebug>
StylesComboPreview::StylesComboPreview(QWidget *parent) :
QLineEdit(parent),
m_renamingNewStyle(false),
m_shouldAddNewStyle(false),
m_addButton(0)
{
init();
}
StylesComboPreview::~StylesComboPreview()
{
delete m_addButton;
m_addButton = 0;
}
void StylesComboPreview::init()
{
setReadOnly(true);
if (m_addButton) {
return;
}
m_addButton = new QPushButton(this);
m_addButton->setCursor( Qt::ArrowCursor );
m_addButton->setIcon(KIcon("list-add"));
m_addButton->setFlat(true);
m_addButton->setMinimumSize(16,16);
m_addButton->setMaximumSize(16, 16);
//TODO uncomment the following line after string freeze, also remove the _ in the middle of i18_nc call
// m_addButton->setToolTip( i18_nc( "@action:button Create a new style with the current properties", "Create style" ) );
connect(m_addButton, SIGNAL(clicked()), this, SLOT(addNewStyle()));
updateAddButton();
}
void StylesComboPreview::updateAddButton()
{
if (!m_addButton) {
return;
}
const QSize geom = size();
const int buttonWidth = m_addButton->size().width();
m_addButton->move(geom.width() - buttonWidth , (geom.height()-m_addButton->size().height())/2);
}
void StylesComboPreview::setAddButtonShown(bool show)
{
m_addButton->setVisible(show);
}
QSize StylesComboPreview::availableSize() const
{
return QSize(contentsRect().width()- m_addButton->width(), contentsRect().height()); ///TODO dynamic resizing when button shown/hidden.
}
void StylesComboPreview::setPreview(QImage image)
{
m_stylePreview = image;
}
bool StylesComboPreview::isAddButtonShown() const
{
return m_addButton != 0;
}
void StylesComboPreview::resizeEvent( QResizeEvent * ev )
{
QLineEdit::resizeEvent(ev);
emit resized();
updateAddButton();
}
void StylesComboPreview::keyPressEvent( QKeyEvent *e )
{
if (m_shouldAddNewStyle && e->key() == Qt::Key_Escape) {
m_renamingNewStyle = false;
m_shouldAddNewStyle = false;
setReadOnly(true);
setText(QString());
e->accept();
}
else if (m_shouldAddNewStyle && (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return)) {
m_renamingNewStyle = false;
m_shouldAddNewStyle = false;
emit newStyleRequested(text());
setReadOnly(true);
setText(QString());
e->accept();
}
else {
QLineEdit::keyPressEvent(e);
}
}
void StylesComboPreview::focusOutEvent(QFocusEvent *e)
{
if (e->reason() != Qt::ActiveWindowFocusReason && e->reason() != Qt::PopupFocusReason) {
if (m_shouldAddNewStyle) {
m_renamingNewStyle = false;
m_shouldAddNewStyle = false;
emit newStyleRequested(text());
setReadOnly(true);
setText(QString());
e->accept();
}
setReadOnly(true);
m_renamingNewStyle = false;
setText(QString());
}
else {
QLineEdit::focusOutEvent(e);
}
}
void StylesComboPreview::mouseReleaseEvent(QMouseEvent *event)
{
Q_UNUSED(event);
if (!m_renamingNewStyle) {
emit clicked();
}
}
void StylesComboPreview::paintEvent( QPaintEvent *ev )
{
if (!m_renamingNewStyle) {
QLineEdit::paintEvent(ev);
QPainter p(this);
p.setClipRect(ev->rect());
p.drawImage(contentsRect().topLeft(), m_stylePreview);
}
else {
QLineEdit::paintEvent(ev);
}
}
void StylesComboPreview::addNewStyle()
{
m_renamingNewStyle = true;
m_shouldAddNewStyle = true;
setText(i18n("New style"));
selectAll();
setReadOnly(false);
this->setFocus();
}
#include "StylesComboPreview.moc"
|