[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: [RFC] KInformationLabel
From:       Michaël_Larouche <larouche () kde ! org>
Date:       2007-03-12 17:59:36
Message-ID: 200703121359.38777.larouche () kde ! org
[Download RAW message or body]

[Attachment #2 (multipart/mixed)]


Le March 12, 2007 04:15, Simon Hausmann a écrit :
> On Monday 12 March 2007 03:17:41 Michaël Larouche wrote:
> > Le March 3, 2007 12:56, Michaël Larouche a écrit :
> > > I have been working (slowy) on a new widget for kdelibs.
> > >
> > > It is a small label to display message in a dialog. The label is first
> > > hidden when instancied and only show up when a message is affected to
> > > the label. This is similiar to the ErrorNotifier control in Windows
> > > Forms. This widget will be useful to replace message box in dialog like
> > > Ask Password dialog and other non-modal dialogs.
> > >
> > > This is a work in progress of course and not all planned features are
> > > not implemented. I would like to add a autohide timeout to the widget.
> > > I tried to add FadeIn animation.
> > >
> > > I would like comments and suggestion about for this widget.
> >
> > Here a updated patch ready to be comitted on tomorrow (kdelibs breakage)
>
> Looks okay to me. Here's some more feedback on the API:
>
> * I suggest to replace setIconName(const QString &name) with just a second
> setIcon overload that takes a QIcon. It's nice if a property called "icon"
> also takes the native icon type :)
>
> * I also wouldn't make setIcon a slot. I've never seen a signal that emits
> an icon :)
>
> * I suggest to rename the timeout property (what's a timeout a label?) to
> autoHideTimeout (aha!).
>
> And of course the obligatory question for kdelibs inclusion at the end:
> Which applications are using this widget (either at the moment or in the
> near future)?
>
>
> Simon

New patch based on your suggestion Simon.

I'll try to make use of this widget where it is needed to replace modal 
message box used in wrong contextes. I needed that for a dialog in Kopete at 
least and kdesu will make good use of it :)

Just think every dialog that ask a password, it will be able to display the 
error meessage right in the dialog in a nice way.
-- 
Michaël Larouche
KDE developer working on Kopete, Gamefu(KDE), Solid...on dial-up :P
--------------------------------------
Website: http://www.tehbisnatch.org/
MSN: michael.larouche@kdemail.net
IRC: irc.freenode.org/DarkShock
Jabber/email: larouche@kde.org

["kinformationlabel_diff_v3.patch" (text/x-diff)]

Index: kdewidgets/kde.widgets
===================================================================
--- kdewidgets/kde.widgets	(révision 641529)
+++ kdewidgets/kde.widgets	(copie de travail)
@@ -264,3 +264,8 @@
 ToolTip=Group of radio buttons with index selection.
 IsContainer=true
 Group=Container (KDE)
+
+[KInformationLabel]
+IncludeFile=kinformationlabel.h
+ToolTip=A specialized label to display informative message
+Group=Display (KDE)
Index: kdeui/tests/kinformationlabeltest.cpp
===================================================================
--- kdeui/tests/kinformationlabeltest.cpp	(révision 0)
+++ kdeui/tests/kinformationlabeltest.cpp	(révision 0)
@@ -0,0 +1,100 @@
+/*  This file is part of the KDE libraries
+    Copyright (C) 2007 Michaël Larouche <larouche@kde.org>
+
+    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; version 2
+    of the License.
+
+    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 "kinformationlabeltest.h"
+
+#include <QtGui/QVBoxLayout>
+#include <QtGui/QHBoxLayout>
+#include <QtGui/QLabel>
+#include <QtGui/QSpinBox>
+#include <QtGui/QPushButton>
+#include <QtGui/QComboBox>
+#include <QtGui/QLineEdit>
+
+#include <kinformationlabel.h>
+#include <kapplication.h>
+#include <kcmdlineargs.h>
+#include <kicon.h>
+
+KInformationLabel_Test::KInformationLabel_Test(QWidget *parent)
+ : QWidget(parent)
+{
+    QVBoxLayout *mainLayout = new QVBoxLayout(this);
+
+    iconComboBox = new QComboBox(this);
+    iconComboBox->addItem( "Information" );
+    iconComboBox->addItem( "Error" );
+    iconComboBox->addItem( "Warning" );
+    iconComboBox->addItem( "Custom" );
+    mainLayout->addWidget( iconComboBox );
+
+    QHBoxLayout *iconNameLayout = new QHBoxLayout(this);
+
+    QLabel *labelIconName = new QLabel("Icon name:", this);
+    iconNameLayout->addWidget(labelIconName);
+
+    lineIconName = new QLineEdit(this);
+    iconNameLayout->addWidget(lineIconName);
+
+    mainLayout->addLayout(iconNameLayout);
+
+    QHBoxLayout *timeoutLayout = new QHBoxLayout(this);
+
+    QLabel *labelTimeout = new QLabel("Autohide timeout:", this);
+    timeoutLayout->addWidget(labelTimeout);
+
+    timeoutSpinBox = new QSpinBox(this);
+    timeoutSpinBox->setRange(0, 10000);
+    timeoutLayout->addWidget(timeoutSpinBox);
+
+    mainLayout->addLayout(timeoutLayout);
+
+    // Create main layout
+    QPushButton *buttonTest = new QPushButton("Show message", this);
+    connect(buttonTest, SIGNAL(clicked()), this, SLOT(buttonTestClicked()));
+    mainLayout->addWidget(buttonTest);
+
+    testLabel = new KInformationLabel(this);
+    mainLayout->addWidget(testLabel);
+    mainLayout->addStretch();
+}
+
+void KInformationLabel_Test::buttonTestClicked()
+{
+    testLabel->setIconType( static_cast<KInformationLabel::Icon>( \
iconComboBox->currentIndex() ) ); +    testLabel->setIcon( \
KIcon(lineIconName->text()) ); +    testLabel->setAutoHideTimeout( \
timeoutSpinBox->value() ); +    testLabel->setText( "This is a test message" );
+}
+
+int main(int argc, char **argv)
+{
+    KCmdLineArgs::init(argc, argv, "kinformationlabeltest", \
"KInformationLabel_Test", "description", "version"); +
+    KApplication app;
+
+    KInformationLabel_Test *mainWidget = new KInformationLabel_Test;
+    mainWidget->setAttribute( static_cast<Qt::WidgetAttribute>(Qt::WA_DeleteOnClose \
| Qt::WA_QuitOnClose) ); +    mainWidget->show();
+
+    return app.exec();
+}
+
+#include "kinformationlabeltest.moc"
+
+// kate: space-indent on; indent-width 4; encoding utf-8; replace-tabs on;
Index: kdeui/tests/kinformationlabeltest.h
===================================================================
--- kdeui/tests/kinformationlabeltest.h	(révision 0)
+++ kdeui/tests/kinformationlabeltest.h	(révision 0)
@@ -0,0 +1,46 @@
+/*  This file is part of the KDE libraries
+    Copyright (C) 2007 Michaël Larouche <larouche@kde.org>
+
+    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; version 2
+    of the License.
+
+    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.
+*/
+#ifndef KINFORMATIONLABELTEST_H
+#define KINFORMATIONLABELTEST_H
+
+#include <QtGui/QWidget>
+
+class KInformationLabel;
+class QSpinBox;
+class QComboBox;
+class QLineEdit;
+
+class KInformationLabel_Test : public QWidget
+{
+    Q_OBJECT
+public:
+    KInformationLabel_Test(QWidget *parent = 0);
+
+private Q_SLOTS:
+    void buttonTestClicked();
+
+private:
+    KInformationLabel *testLabel;
+    QSpinBox *timeoutSpinBox;
+    QComboBox *iconComboBox;
+    QLineEdit *lineIconName;
+};
+
+#endif
+// kate: space-indent on; indent-width 4; encoding utf-8; replace-tabs on;
Index: kdeui/tests/CMakeLists.txt
===================================================================
--- kdeui/tests/CMakeLists.txt	(révision 641529)
+++ kdeui/tests/CMakeLists.txt	(copie de travail)
@@ -216,6 +216,18 @@
 
 ########### next target ###############
 
+set(kinformationlabeltest_SRCS
+kinformationlabeltest.cpp
+)
+
+kde4_automoc(${kinformationlabeltest_SRCS})
+
+kde4_add_executable(kinformationlabeltest NOGUI RUN_UNINSTALLED \
${kinformationlabeltest_SRCS}) +
+target_link_libraries(kinformationlabeltest ${KDE4_KDEUI_LIBS})
+
+########### next target ###############
+
 set(kledtest_SRCS
 kledtest.cpp
 )
Index: kdeui/widgets/kinformationlabel.cpp
===================================================================
--- kdeui/widgets/kinformationlabel.cpp	(révision 0)
+++ kdeui/widgets/kinformationlabel.cpp	(révision 0)
@@ -0,0 +1,218 @@
+/*  This file is part of the KDE libraries
+    Copyright (C) 2007 Michaël Larouche <larouche@kde.org>
+
+    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; version 2
+    of the License.
+
+    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 "kinformationlabel.h"
+
+// Qt includes
+#include <QtCore/QLatin1String>
+#include <QtCore/QTimer>
+#include <QtGui/QLabel>
+#include <QtGui/QHBoxLayout>
+#include <QtGui/QMouseEvent>
+
+// KDE includes
+#include <kdebug.h>
+#include <kicon.h>
+
+class KInformationLabel::Private
+{
+public:
+    Private(KInformationLabel *parent)
+     : q(parent), iconLabel(0), messageLabel(0), \
currentIconType(KInformationLabel::Information), +       autoHideTimeout(0)
+    {}
+
+    KInformationLabel *q;
+    QLabel *iconLabel;
+    QLabel *messageLabel;
+
+    KInformationLabel::Icon currentIconType;
+    QIcon icon;
+    int autoHideTimeout;
+
+    /**
+     * @brief Get the icon name from the icon type
+     * @param type icon type from the enum
+     * @return named icon as QString
+     */
+    QString iconTypeToIconName(KInformationLabel::Icon type);
+    /**
+     * @brief Update the icon for the label
+     * @param icon a stock icon loaded using KIcon
+     */
+    void updateIcon(const KIcon &icon);
+
+    void showWidget();
+    void _k_timeoutFinished();
+};
+
+KInformationLabel::KInformationLabel(QWidget *parent)
+ : QFrame(parent), d(new Private(this))
+{
+    // Set QFrame parameters
+    setAutoFillBackground(true);
+    setFrameShape(QFrame::StyledPanel);
+    setFrameShadow(QFrame::Plain);
+    setBackgroundRole(QPalette::Base);
+
+    // Create icon label
+    d->iconLabel = new QLabel(this);
+    d->iconLabel->installEventFilter(this);
+    d->iconLabel->setFixedSize(24, 24);
+
+    // Create message label
+    d->messageLabel = new QLabel(this);
+    d->messageLabel->installEventFilter(this);
+    d->messageLabel->setAlignment(Qt::AlignLeft);
+    d->messageLabel->setStyleSheet( QLatin1String("QLabel { font-weight: bold }") );
+
+    QHBoxLayout *mainLayout = new QHBoxLayout;
+    mainLayout->setMargin(3);
+    mainLayout->addWidget(d->iconLabel,0,Qt::AlignVCenter);
+    mainLayout->addWidget(d->messageLabel,0,Qt::AlignVCenter);
+    mainLayout->addStretch(1);
+
+    setLayout(mainLayout);
+
+    // By default, the label is hidden for view and will only be
+    // visible when a message
+    setVisible(false);
+
+    installEventFilter(this);
+
+    // Set default icon (for designer)
+    setIconType( KInformationLabel::Information );
+}
+
+KInformationLabel::~KInformationLabel()
+{
+    delete d;
+}
+
+bool KInformationLabel::eventFilter(QObject *object, QEvent *event)
+{
+    // Hide message label on click
+    if( event->type() == QEvent::MouseButtonPress )
+    {
+        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
+        if( mouseEvent && mouseEvent->button() == Qt::LeftButton )
+        {
+            setVisible(false);
+            return true;
+        }
+    }
+
+    return QFrame::eventFilter(object, event);
+}
+
+QString KInformationLabel::text() const
+{
+    return d->messageLabel->text();
+}
+
+KInformationLabel::Icon KInformationLabel::iconType() const
+{
+    return d->currentIconType;
+}
+
+QIcon KInformationLabel::icon() const
+{
+    return d->icon;
+}
+
+int KInformationLabel::autoHideTimeout() const
+{
+    return d->autoHideTimeout;
+}
+
+void KInformationLabel::setText(const QString &text)
+{
+    if( !text.isEmpty() )
+    {
+        d->messageLabel->setText( text );
+        d->showWidget();
+    }
+    else
+    {
+        setVisible(false);
+    }
+}
+
+void KInformationLabel::setIconType(KInformationLabel::Icon iconType)
+{
+    d->currentIconType = iconType;
+    d->updateIcon( KIcon(d->iconTypeToIconName(iconType)) );
+}
+
+void KInformationLabel::setIcon(const QIcon &icon)
+{
+    d->icon = icon;
+    if( d->currentIconType == KInformationLabel::Custom )
+    {
+        d->updateIcon( KIcon(icon) );
+    }
+}
+
+void KInformationLabel::setAutoHideTimeout(int msecs)
+{
+    d->autoHideTimeout = msecs;
+}
+
+void KInformationLabel::Private::updateIcon(const KIcon &icon)
+{
+    iconLabel->setPixmap( icon.pixmap(24, 24) );
+}
+
+QString KInformationLabel::Private::iconTypeToIconName(KInformationLabel::Icon \
iconType) +{
+    QString icon;
+
+    switch(iconType)
+    {
+        case KInformationLabel::Information:
+            icon = QLatin1String("dialog-information");
+            break;
+        case KInformationLabel::Error:
+            icon = QLatin1String("dialog-error");
+            break;
+        case KInformationLabel::Warning:
+            icon = QLatin1String("dialog-warning");
+            break;
+        case KInformationLabel::Custom:
+            break;
+    }
+
+    return icon;
+}
+
+void KInformationLabel::Private::_k_timeoutFinished()
+{
+    q->setVisible(false);
+}
+
+void KInformationLabel::Private::showWidget()
+{
+    q->setVisible(true);
+    if( autoHideTimeout > 0 )
+    {
+        QTimer::singleShot(autoHideTimeout, q, SLOT(_k_timeoutFinished()));
+    }
+}
+
+#include "kinformationlabel.moc"
+// kate: space-indent on; indent-width 4; encoding utf-8; replace-tabs on;
Index: kdeui/widgets/kinformationlabel.h
===================================================================
--- kdeui/widgets/kinformationlabel.h	(révision 0)
+++ kdeui/widgets/kinformationlabel.h	(révision 0)
@@ -0,0 +1,185 @@
+/*  This file is part of the KDE libraries
+    Copyright (C) 2007 Michaël Larouche <larouche@kde.org>
+
+    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; version 2
+    of the License.
+
+    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.
+*/
+#ifndef KDEUI_KINFORMATIONLABEL_H
+#define KDEUI_KINFORMATIONLABEL_H
+
+#include <QtGui/QFrame>
+#include <QtGui/QIcon>
+#include <kdelibs_export.h>
+
+class QMouseEvent;
+/**
+ * @brief A specialized label to display informative message
+ *
+ * KInformationLabel is used to display informative message only when needed.
+ * This is useful to display a warning or error message in a dialog to notify
+ * the user of a problem without using a modal dialog like a message box.
+ *
+ * @section Behavior
+ * KInformationLabel has a special behavior. When first instancied,
+ * It is not visible to the user untill a message is affected to it.
+ *
+ * After the message being shown, the user can click on the label to hide
+ * it. Also, you can set a autohide timeout using the timeout property.
+ * By default, no timeout is specified so the label is shown untill the
+ * user click on it.
+ *
+ * To hide the widget using code, just set an empty text.
+ *
+ * @section Usage
+ * KInformationLabel is available as a widget in Qt Designer. Usage is very
+ * simple, you just need to set a text to the label. 4 types of messages can
+ * be set to the label, an informative message, an error message, a warning
+ * message and a custom message with a custom icon. See the example below:
+ * @code
+KInformationLabel *label = new KInformationLabel(parent);
+label->setIconType(KInformation::Error);
+label->setText("Sample Error Message");
+ * @endcode
+ *
+ * To set a custom message and icon, you need to set the icon property
+ * using setIcon() and iconType property to Custom using setIconType
+ * @code
+KInformationLabel *label = new KInformationLabel(parent);
+label->setIconType(KInformation::Custom);
+label->setIcon( KIcon("go-home") );
+label->setText("Custom message using go-home icon");
+ * @endcode
+ *
+ * @author Michaël Larouche <larouche@kde.org>
+ */
+class KDEUI_EXPORT KInformationLabel : public QFrame
+{
+    Q_OBJECT
+    /**
+     * @brief Text used in the label
+     */
+    Q_PROPERTY(QString text READ text WRITE setText)
+    /**
+     * @brief Icon type for the message
+     */
+    Q_PROPERTY(Icon iconType READ iconType WRITE setIconType)
+    /**
+     * @brief Icon for the Custom icon type
+     */
+    Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
+    /**
+     * @brief Autohide timeout for the label. Set to 0 to disable autohide
+     */
+    Q_PROPERTY(int autoHideTimeout READ autoHideTimeout WRITE setAutoHideTimeout)
+    Q_ENUMS(Icon)
+
+public:
+    /**
+     * Type of icons which can be displayed by the label
+     */
+    enum Icon
+    {
+        Information, ///< Icon for an informative message
+        Error, ///< Icon for an error message
+        Warning, ///< Icon for a warning message
+        Custom ///< Set a custom icon using setIconName()
+    };
+
+    /**
+     * Constructor
+     * @param parent Parent widget
+     */
+    explicit KInformationLabel(QWidget *parent = 0);
+    /**
+     * Destructor
+     */
+    ~KInformationLabel();
+
+    /**
+     * @brief Get the current text set in the label
+     * @return current text as QString
+     */
+    QString text() const;
+
+    /**
+     * @brief Get the current icon type
+     * @return current icon type
+     */
+    KInformationLabel::Icon iconType() const;
+
+    /**
+     * @brief Get the current icon name.
+     * @return current icon name
+     */
+    QIcon icon() const;
+
+    /**
+     * @brief Get the current timeout value in miliseconds
+     * @return timeout value in msecs.
+     */
+    int autoHideTimeout() const;
+
+    /**
+     * @brief Set an icon type to the label
+     *
+     * When icon type is set to Custom, it use the current
+     * icon name to update the pixmap in the label.
+     * @param iconType current icon type. See Icon enum
+     */
+    void setIconType(KInformationLabel::Icon iconType);
+
+    /**
+     * @brief Set an icon for Custom icon type
+     *
+     * When the label is in Custom mode, it also
+     * update the current pixmap.
+     * @param icon a QIcon or a KIcon
+     */
+    void setIcon(const QIcon &icon);
+
+public Q_SLOTS:
+    /**
+     * @brief Set a message to the label
+     *
+     * When calling this method, the widget get visible.
+     *
+     * If the text is empty, the widget hide itself.
+     *
+     * @param text text to show. use an empty string to hide the widget
+     */
+    void setText(const QString &text);
+
+    /**
+     * @brief Set the autohide timeout of the label
+     *
+     * Set value to 0 to disable autohide.
+     * @param msecs timeout value in milliseconds
+     */
+    void setAutoHideTimeout(int msecs);
+
+protected:
+    bool eventFilter(QObject *object, QEvent *event);
+
+private:
+    Q_DISABLE_COPY(KInformationLabel)
+    Q_PRIVATE_SLOT(d, void _k_timeoutFinished())
+
+    class Private;
+    Private * const d;
+};
+
+#endif
+
+// kate: space-indent on; indent-width 4; encoding utf-8; replace-tabs on;
Index: kdeui/CMakeLists.txt
===================================================================
--- kdeui/CMakeLists.txt	(révision 641529)
+++ kdeui/CMakeLists.txt	(copie de travail)
@@ -148,6 +148,7 @@
  widgets/khbox.cpp
  widgets/khelpmenu.cpp
  widgets/kkeybutton.cpp
+ widgets/kinformationlabel.cpp
  widgets/kled.cpp
  widgets/klineedit.cpp
  widgets/kmenu.cpp
@@ -339,6 +340,7 @@
  widgets/khbox.h
  widgets/khelpmenu.h
  widgets/kkeybutton.h
+ widgets/kinformationlabel.h
  widgets/kled.h
  widgets/klineedit.h
  widgets/kmenu.h


[Attachment #6 (application/pgp-signature)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic