Skip to content

Commit

Permalink
Create a new ClearButton class from scratch that will show the KDE clear
Browse files Browse the repository at this point in the history
location bar icon rather then the circle with X.
This new class is released under the BSD license.
  • Loading branch information
icefox committed Oct 7, 2009
1 parent f8b4fe0 commit 330d24c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 149 deletions.
152 changes: 67 additions & 85 deletions src/clearbutton.cpp
@@ -1,110 +1,92 @@
/*
* Copyright 2008 Benjamin C. Meyer <ben@meyerhome.net>
/**
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
*
* 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.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Benjamin Meyer nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

/****************************************************************************
**
** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. In
** addition, as a special exception, Trolltech gives you certain
** additional rights. These rights are described in the Trolltech GPL
** Exception version 1.2, which can be found at
** http://www.trolltech.com/products/qt/gplexception/ and in the file
** GPL_EXCEPTION.txt in this package.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "clearbutton.h"

#include <qpainter.h>
/*
#include <qevent.h>
#include <qmenu.h>
#include <qstyle.h>
#include <qstyleoption.h>

#include <qdebug.h>
*/
ClearButton::ClearButton(QWidget *parent)
: QAbstractButton(parent)
{
setCursor(Qt::ArrowCursor);
setToolTip(tr("Clear"));
setVisible(false);
setFocusPolicy(Qt::NoFocus);
setToolTip(tr("Clear"));
setMinimumSize(22, 22);
setVisible(false);

#if QT_VERSION >= 0x040600
// First check for a style icon, current KDE provides one
if (m_styleImage.isNull()) {
QLatin1String iconName = (layoutDirection() == Qt::RightToLeft)
? QLatin1String("edit-clear-locationbar-ltr")
: QLatin1String("edit-clear-locationbar-rtl");
QIcon icon = QIcon::fromTheme(iconName);
if (!icon.isNull())
m_styleImage = icon.pixmap(16, 16).toImage();
}
#endif
}

void ClearButton::textChanged(const QString &text)
{
setVisible(!text.isEmpty());
}

void ClearButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
int height = this->height();

if (!m_styleImage.isNull()) {
int x = (width() - m_styleImage.width()) / 2 - 1;
int y = (height() - m_styleImage.height()) / 2 - 1;
painter.drawImage(x, y, m_styleImage);
return;
}

// Fall back to boring circle X
painter.setRenderHint(QPainter::Antialiasing, true);
QColor color = palette().color(QPalette::Mid);
painter.setBrush(isDown()
? palette().color(QPalette::Dark)
: palette().color(QPalette::Mid));
painter.setPen(painter.brush().color());
int size = width();
int offset = size / 5;
int radius = size - offset * 2;
painter.drawEllipse(offset, offset, radius, radius);

painter.setPen(palette().color(QPalette::Base));
int border = offset * 2;
painter.drawLine(border, border, width() - border, height - border);
painter.drawLine(border, height - border, width() - border, border);
}
QPalette p = palette();
QColor circleColor = isDown() ? p.color(QPalette::Dark) : p.color(QPalette::Mid);
QColor xColor = p.color(QPalette::Window);

void ClearButton::textChanged(const QString &text)
{
setVisible(!text.isEmpty());
// draw circle
painter.setBrush(circleColor);
painter.setPen(circleColor);
int padding = width() / 5;
int circleRadius = width() - (padding * 2);
painter.drawEllipse(padding, padding, circleRadius, circleRadius);

// draw X
painter.setPen(xColor);
padding = padding * 2;
painter.drawLine(padding, padding, width() - padding, width() - padding);
painter.drawLine(padding, height() - padding, width() - padding, padding);
}

94 changes: 30 additions & 64 deletions src/clearbutton.h
@@ -1,85 +1,51 @@
/*
* Copyright 2008 Benjamin C. Meyer <ben@meyerhome.net>
/**
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
*
* 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.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Benjamin Meyer nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

/****************************************************************************
**
** Copyright (C) 2007-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. In
** addition, as a special exception, Trolltech gives you certain
** additional rights. These rights are described in the Trolltech GPL
** Exception version 1.2, which can be found at
** http://www.trolltech.com/products/qt/gplexception/ and in the file
** GPL_EXCEPTION.txt in this package.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef CLEARBUTTON_H
#define CLEARBUTTON_H

#include <qabstractbutton.h>

/*
Clear button on the right hand side of the search widget.
Hidden by default
"A circle with an X in it"
*/
class ClearButton : public QAbstractButton
{
Q_OBJECT

public:
ClearButton(QWidget *parent = 0);
void paintEvent(QPaintEvent *event);

public slots:
void textChanged(const QString &text);

protected:
void paintEvent(QPaintEvent *event);

private:
QImage m_styleImage;
};

#endif // CLEARBUTTON_H
Expand Down

0 comments on commit 330d24c

Please sign in to comment.