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

List:       kde-core-devel
Subject:    Re: [Patch] KTitleWidget
From:       Urs Wolfer <uwolfer () kde ! org>
Date:       2007-05-18 12:21:57
Message-ID: 200705181422.01584.uwolfer () kde ! org
[Download RAW message or body]

[Attachment #2 (multipart/mixed)]


Hi

I have updated my patch with the suggested changes.

Now KTitleWidget has a text and a pixmap property. This allows to use this 
widget in most of the use cases without any other widgets inside it. Anyway, 
if this not enough, we can use the setWidget method to show own widgets in 
the title widget (mkretz: IHMO this makes sense; see for example 
QScrollArea::setWidget).

See the KTitleWidget test in order to see how it works exactly.

Bye
urs

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

Index: includes/CMakeLists.txt
===================================================================
--- includes/CMakeLists.txt	(revision 665940)
+++ includes/CMakeLists.txt	(working copy)
@@ -470,6 +470,7 @@
   KTimeZones
   KTipDatabase
   KTipDialog
+  KTitleWidget
   KToggleAction
   KToggleFullScreenAction
   KToggleToolBarAction
Index: includes/KTitleWidget
===================================================================
--- includes/KTitleWidget	(revision 0)
+++ includes/KTitleWidget	(revision 0)
@@ -0,0 +1 @@
+#include "../ktitlewidget.h"
Index: kdeui/paged/kpageview.cpp
===================================================================
--- kdeui/paged/kpageview.cpp	(revision 665940)
+++ kdeui/paged/kpageview.cpp	(working copy)
@@ -26,6 +26,7 @@
 
 #include <kdialog.h>
 #include <kiconloader.h>
+#include <ktitlewidget.h>
 
 #include <QAbstractItemView>
 #include <QGridLayout>
@@ -73,9 +74,7 @@
 
     // gui
     KPageStackedWidget *stack;
-    QFrame *headerFrame;
-    QLabel *headerLabel;
-    QLabel *headerIcon;
+    KTitleWidget *titleWidget;
     QGridLayout *layout;
 
     QAbstractItemView *view;
@@ -119,7 +118,7 @@
       view->selectionModel()->setCurrentIndex( model->index( 0, 0 ), \
QItemSelectionModel::Select );  }
 
-  headerFrame->setVisible( parent->showPageHeader() );
+  titleWidget->setVisible(parent->showPageHeader());
 
   Qt::Alignment alignment = parent->viewPosition();
   if ( alignment & Qt::AlignTop )
@@ -271,10 +270,10 @@
   if ( header.isEmpty() ) {
     header = model->data( index, Qt::DisplayRole ).toString();
   }
-  headerLabel->setText( header );
+  titleWidget->setText(header);
 
   const QIcon icon = model->data( index, Qt::DecorationRole ).value<QIcon>();
-  headerIcon->setPixmap( icon.pixmap( 22, 22 ) );
+  titleWidget->setPixmap(icon.pixmap(22, 22));
 
   emit parent->currentPageChanged( index, previous );
 }
@@ -296,10 +295,10 @@
   if ( header.isEmpty() ) {
     header = model->data( index, Qt::DisplayRole ).toString();
   }
-  headerLabel->setText( header );
+  titleWidget->setText(header);
 
   const QIcon icon = model->data( index, Qt::DecorationRole ).value<QIcon>();
-  headerIcon->setPixmap( icon.pixmap( 22, 22 ) );
+  titleWidget->setPixmap(icon.pixmap(22, 22));
 }
 
 /**
@@ -310,27 +309,10 @@
 {
   d->layout = new QGridLayout( this );
   d->stack = new KPageStackedWidget( this );
-  d->headerLabel = new QLabel( this );
-  d->headerIcon = new QLabel( this );
 
-  d->headerLabel->setStyleSheet( "QLabel { font-weight: bold; }" );
+  d->titleWidget = new KTitleWidget(this);
 
-  d->headerFrame = new QFrame( this );
-  d->headerFrame->setFrameShape( QFrame::StyledPanel );
-  d->headerFrame->setFrameShadow( QFrame::Plain );
-
-  d->headerFrame->setAutoFillBackground( true );
-  d->headerFrame->setBackgroundRole( QPalette::Base );
-
-  QHBoxLayout *headerLayout = new QHBoxLayout();
-  // use spacingHint (6 pixel), looks much better than marginHint
-  headerLayout->setMargin( KDialog::spacingHint() );
-  headerLayout->addWidget( d->headerLabel );
-  headerLayout->addWidget( d->headerIcon );
-  headerLayout->setStretchFactor( d->headerLabel, 1 );
-  d->headerFrame->setLayout( headerLayout );
-
-  d->layout->addWidget( d->headerFrame, 1, 1 );
+  d->layout->addWidget(d->titleWidget, 1, 1);
   d->layout->addWidget( d->stack, 2, 1 );
 
   // stack should use most space
Index: kdeui/tests/ktitlewidgettest.cpp
===================================================================
--- kdeui/tests/ktitlewidgettest.cpp	(revision 0)
+++ kdeui/tests/ktitlewidgettest.cpp	(revision 0)
@@ -0,0 +1,73 @@
+/* This file is part of the KDE libraries
+   Copyright (C) 2007 Urs Wolfer <uwolfer @ 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 version 2 as published by the Free Software Foundation.
+
+   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 <kapplication.h>
+#include <kcmdlineargs.h>
+#include <kicon.h>
+#include <ktitlewidget.h>
+
+#include <QCheckBox>
+#include <QLabel>
+#include <QVBoxLayout>
+#include <QWidget>
+
+class KTitleWidgetTestWidget : public QWidget
+{
+public:
+    KTitleWidgetTestWidget(QWidget *parent = 0)
+     : QWidget(parent)
+    {
+        QVBoxLayout *mainLayout = new QVBoxLayout(this);
+
+        KTitleWidget *titleWidget = new KTitleWidget(this);
+        titleWidget->setText("Title");
+        titleWidget->setPixmap(KIcon("screen").pixmap(22, 22), \
KTitleWidget::ImageLeft); +
+        mainLayout->addWidget(titleWidget);
+
+        KTitleWidget *checkboxTitleWidget = new KTitleWidget(this);
+
+        QWidget *checkBoxTitleMainWidget = new QWidget(this);
+        QVBoxLayout *titleLayout = new QVBoxLayout(checkBoxTitleMainWidget);
+        titleLayout->setMargin(6);
+
+        QCheckBox *checkBox = new QCheckBox("Text Checkbox", \
checkBoxTitleMainWidget); +        titleLayout->addWidget(checkBox);
+        checkboxTitleWidget->setWidget(checkBoxTitleMainWidget);
+
+        mainLayout->addWidget(checkboxTitleWidget);
+
+        QLabel *otherLabel = new QLabel("Some text...", this);
+
+        mainLayout->addWidget(otherLabel);
+
+        mainLayout->addStretch();
+    }
+};
+
+int main(int argc, char **argv)
+{
+    KCmdLineArgs::init(argc, argv, "ktitlewidgettest", "KTitleWidgetTest", \
"description", "version"); +
+    KApplication app;
+
+    KTitleWidgetTestWidget *mainWidget = new KTitleWidgetTestWidget;
+    mainWidget->show();
+
+    return app.exec();
+}
Index: kdeui/tests/CMakeLists.txt
===================================================================
--- kdeui/tests/CMakeLists.txt	(revision 665940)
+++ kdeui/tests/CMakeLists.txt	(working copy)
@@ -106,6 +106,7 @@
   kpassworddialogtest
   kjobtrackerstest
   kdebugtest_gui
+  ktitlewidgettest
 )
 
 if (Q_WS_X11)
Index: kdeui/CMakeLists.txt
===================================================================
--- kdeui/CMakeLists.txt	(revision 665940)
+++ kdeui/CMakeLists.txt	(working copy)
@@ -194,6 +194,7 @@
  widgets/ktextbrowser.cpp
  widgets/ktextedit.cpp
  widgets/ktimezonewidget.cpp
+ widgets/ktitlewidget.cpp
  widgets/ktoolbar.cpp
  widgets/kurllabel.cpp
  widgets/kvbox.cpp
@@ -415,6 +416,7 @@
  widgets/ktextbrowser.h
  widgets/ktextedit.h
  widgets/ktimezonewidget.h
+ widgets/ktitlewidget.h
  widgets/ktabbar.h
  widgets/ktabwidget.h
  widgets/kruler.h
Index: kdeui/dialogs/kaboutkdedialog.cpp
===================================================================
--- kdeui/dialogs/kaboutkdedialog.cpp	(revision 665940)
+++ kdeui/dialogs/kaboutkdedialog.cpp	(working copy)
@@ -30,6 +30,7 @@
 #include <kglobalsettings.h>
 #include <klocale.h>
 #include <kstandarddirs.h>
+#include <ktitlewidget.h>
 
 KAboutKdeDialog::KAboutKdeDialog(QWidget *parent)
   : KDialog(parent),
@@ -41,13 +42,8 @@
     setHelp("khelpcenter/main.html");
     setModal(false);
 
-    QLabel *headerLabel = new QLabel;
-    headerLabel->setMargin(5);
-    headerLabel->setText(i18n("<font size=\"4\">K Desktop \
                Environment</font><br><b>Version %1</b>", \
                QString(KDE_VERSION_STRING)));
-    headerLabel->setFrameShape(QFrame::StyledPanel);
-    headerLabel->setFrameShadow(QFrame::Plain);
-    headerLabel->setAutoFillBackground(true);
-    headerLabel->setBackgroundRole(QPalette::Base);
+    KTitleWidget *titleWidget = new KTitleWidget(this);
+    titleWidget->setText(i18n("<font size=\"4\">K Desktop \
Environment</font><br><b>Version %1</b>", QString(KDE_VERSION_STRING)));  
     QLabel *about = new QLabel;
     about->setMargin(10);
@@ -140,7 +136,7 @@
     midLayout->addWidget(tabWidget);
 
     QVBoxLayout *mainLayout = new QVBoxLayout;
-    mainLayout->addWidget(headerLabel);
+    mainLayout->addWidget(titleWidget);
     mainLayout->addLayout(midLayout);
     mainLayout->setMargin(0);
 
Index: kdeui/dialogs/kaboutapplicationdialog.cpp
===================================================================
--- kdeui/dialogs/kaboutapplicationdialog.cpp	(revision 665940)
+++ kdeui/dialogs/kaboutapplicationdialog.cpp	(working copy)
@@ -34,6 +34,7 @@
 #include <kiconloader.h>
 #include <klocale.h>
 #include <ktextbrowser.h>
+#include <ktitlewidget.h>
 
 class KAboutApplicationDialog::Private
 {
@@ -71,23 +72,15 @@
         return;
     }
 
-    QFrame *titleLabel = new QFrame();
-    titleLabel->setAutoFillBackground(true);
-    titleLabel->setFrameShape(QFrame::StyledPanel);
-    titleLabel->setFrameShadow(QFrame::Plain);
-    titleLabel->setBackgroundRole(QPalette::Base);
+    KTitleWidget *titleWidget = new KTitleWidget(this);
 
-    QLabel *iconLabel = new QLabel(titleLabel);
-    iconLabel->setFixedSize(64, 64);
-    iconLabel->setPixmap(qApp->windowIcon().pixmap(64, 64));
+    titleWidget->setPixmap(qApp->windowIcon().pixmap(64, 64), \
KTitleWidget::ImageLeft);  if (aboutData->programLogo().canConvert<QPixmap>())
-        iconLabel->setPixmap(aboutData->programLogo().value<QPixmap>());
+        titleWidget->setPixmap(aboutData->programLogo().value<QPixmap>(), \
KTitleWidget::ImageLeft);  else if (aboutData->programLogo().canConvert<QImage>())
-        iconLabel->setPixmap(QPixmap::fromImage(aboutData->programLogo().value<QImage>()));
 +        titleWidget->setPixmap(QPixmap::fromImage(aboutData->programLogo().value<QImage>()), \
KTitleWidget::ImageLeft);  
-    QLabel *headerLabel = new QLabel(titleLabel);
-    headerLabel->setAlignment(Qt::AlignLeft);
-    headerLabel->setText(i18n("<font size=\"5\">%1</font><br><b>Version \
%2</b><br>Using KDE %3</html>", aboutData->programName(), +    \
titleWidget->setText(i18n("<font size=\"5\">%1</font><br><b>Version %2</b><br>Using \
                KDE %3</html>", aboutData->programName(),
                          aboutData->version(), QString(KDE_VERSION_STRING)));
 
     QTabWidget *tabWidget = new QTabWidget;
@@ -215,17 +208,8 @@
         tabWidget->addTab(translatorTextBrowser, i18n("T&ranslation"));
     }
 
-    QHBoxLayout *titleLayout = new QHBoxLayout;
-    titleLayout->setMargin(3);
-    titleLayout->setSpacing(KDialog::spacingHint());
-    titleLayout->addWidget(iconLabel,0,Qt::AlignVCenter);
-    titleLayout->addWidget(headerLabel,0,Qt::AlignVCenter);
-    titleLayout->addStretch(1);
-
-    titleLabel->setLayout(titleLayout);
-
     QVBoxLayout *mainLayout = new QVBoxLayout;
-    mainLayout->addWidget(titleLabel);
+    mainLayout->addWidget(titleWidget);
     mainLayout->addWidget(tabWidget);
     mainLayout->setMargin(0);
 
Index: kdeui/widgets/ktitlewidget.cpp
===================================================================
--- kdeui/widgets/ktitlewidget.cpp	(revision 0)
+++ kdeui/widgets/ktitlewidget.cpp	(revision 0)
@@ -0,0 +1,117 @@
+/* This file is part of the KDE libraries
+   Copyright (C) 2007 Urs Wolfer <uwolfer @ 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 version 2 as published by the Free Software Foundation.
+
+   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 "ktitlewidget.h"
+
+#include <QtGui/QFrame>
+#include <QtGui/QLabel>
+#include <QtGui/QLayout>
+#include <QtGui/QTextDocument>
+
+class KTitleWidget::Private
+{
+public:
+    QHBoxLayout *titleWidgetLayout;
+    QHBoxLayout *headerLayout;
+    QLabel *imageLabel;
+    QLabel *textLabel;
+    QWidget *headerWidget;
+};
+
+KTitleWidget::KTitleWidget(QWidget *parent)
+  : QWidget(parent),
+    d(new Private)
+{
+    QFrame *titleFrame = new QFrame(this);
+    titleFrame->setAutoFillBackground(true);
+    titleFrame->setFrameShape(QFrame::StyledPanel);
+    titleFrame->setFrameShadow(QFrame::Plain);
+    titleFrame->setBackgroundRole(QPalette::Base);
+
+    d->titleWidgetLayout = new QHBoxLayout(titleFrame);
+    d->titleWidgetLayout->setMargin(0);
+
+    // default image / text part start
+    d->headerWidget = new QWidget(this);
+    d->headerWidget->setVisible(false);
+    d->headerLayout = new QHBoxLayout(d->headerWidget);
+    d->headerLayout->setMargin(6);
+
+    d->textLabel = new QLabel(d->headerWidget);
+    d->headerLayout->addWidget(d->textLabel, 1);
+
+    d->imageLabel = new QLabel(d->headerWidget);
+    d->headerLayout->addWidget(d->imageLabel);
+
+    d->titleWidgetLayout->addWidget(d->headerWidget);
+    // default image / text part end
+
+    QVBoxLayout *mainLayout = new QVBoxLayout(this);
+    mainLayout->addWidget(titleFrame);
+    mainLayout->setMargin(0);
+    setLayout(mainLayout);
+}
+
+KTitleWidget::~KTitleWidget()
+{
+    delete d;
+}
+
+void KTitleWidget::setWidget(QWidget *widget)
+{
+    d->titleWidgetLayout->addWidget(widget);
+}
+
+QString KTitleWidget::text() const
+{
+    return d->textLabel->text();
+}
+
+const QPixmap *KTitleWidget::pixmap() const
+{
+    return d->imageLabel->pixmap();
+}
+
+void KTitleWidget::setText(const QString &text, Qt::Alignment alignment)
+{
+    if (!Qt::mightBeRichText(text))
+        d->textLabel->setStyleSheet("QLabel { font-weight: bold; }");
+
+    d->textLabel->setText(text);
+
+    d->textLabel->setAlignment(alignment);
+    d->headerWidget->setVisible(true);
+}
+
+void KTitleWidget::setPixmap(const QPixmap &pixmap, ImageAlignment alignment)
+{
+    if (alignment == ImageLeft) {
+        d->headerLayout->removeWidget(d->textLabel); // remove text from 1st \
position... +        d->headerLayout->addWidget(d->textLabel, 1); // ... and move it \
to the end +    }
+
+    if (alignment == ImageRight) {
+        d->headerLayout->removeWidget(d->imageLabel);
+        d->headerLayout->addWidget(d->imageLabel);
+    }
+
+    d->imageLabel->setPixmap(pixmap);
+    d->headerWidget->setVisible(true);
+}
+
+#include "ktitlewidget.moc"
Index: kdeui/widgets/ktitlewidget.h
===================================================================
--- kdeui/widgets/ktitlewidget.h	(revision 0)
+++ kdeui/widgets/ktitlewidget.h	(revision 0)
@@ -0,0 +1,121 @@
+/* This file is part of the KDE libraries
+   Copyright (C) 2007 Urs Wolfer <uwolfer @ 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 version 2 as published by the Free Software Foundation.
+
+   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 KTITLEWIDGET_H
+#define KTITLEWIDGET_H
+
+#include <kdeui_export.h>
+
+#include <QtGui/QWidget>
+
+/**
+ * @short Standard title widget with a white background and round border
+ *
+ * This class provides a widget often used for dialog titles.
+ *
+ * @section Usage
+ * KTitleWidget is very simple to use. You can either use its default text
+ * (and pixmap) properties or display your own widgets in the title widget.
+ *
+ * A title text with a left aligned pixmap:
+ * @code
+KTitleWidget *titleWidget = new KTitleWidget(this);
+titleWidget->setText(i18n("Title"));
+titleWidget->setPixmap(KIcon("screen").pixmap(22, 22), KTitleWidget::ImageLeft);
+ * @endcode
+ *
+ * Use it with a own widget:
+ * @code
+KTitleWidget *checkboxTitleWidget = new KTitleWidget(this);
+
+QWidget *checkBoxTitleMainWidget = new QWidget(this);
+QVBoxLayout *titleLayout = new QVBoxLayout(checkBoxTitleMainWidget);
+titleLayout->setMargin(6);
+
+QCheckBox *checkBox = new QCheckBox("Text Checkbox", checkBoxTitleMainWidget);
+titleLayout->addWidget(checkBox);
+
+checkboxTitleWidget->setWidget(checkBoxTitleMainWidget);
+ * @endcode
+ *
+ * @author Urs Wolfer uwolfer @ kde.org
+ */
+
+class KDEUI_EXPORT KTitleWidget : public QWidget
+{
+    Q_OBJECT
+    Q_ENUMS(ImageAlignment)
+    Q_PROPERTY(QString text READ text WRITE setText)
+    Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap)
+
+public:
+    /**
+     * This is the enumeration of the possible title pixmap alignments.
+     *
+     * @li ImageLeft: Display the pixmap left
+     * @li ImageRight: Display the pixmap right (default)
+     */
+    enum ImageAlignment {
+        ImageLeft,
+        ImageRight
+    };
+
+    /**
+     * Constructs a progress bar with the given @param parent.
+     */
+    explicit KTitleWidget(QWidget *parent = 0);
+
+    virtual ~KTitleWidget();
+
+    /**
+     * @param widget Widget displayed on the title widget.
+     */
+    void setWidget(QWidget *widget);
+
+    /**
+     * @return the text displayed in the title
+     */
+    QString text() const;
+
+    /**
+     * @return the pixmap displayed in the title
+     */
+    const QPixmap *pixmap() const;
+
+public Q_SLOTS:
+    /**
+     * @param text Text displayed on the label. It can either be plain text or rich \
text. If it +     * is plain text, the text is displayed as a bold title text.
+     * @param alignment Alignment of the text. Default is left and vertical \
centered. +     */
+    void setText(const QString &text, Qt::Alignment alignment = Qt::AlignLeft | \
Qt::AlignVCenter); +
+    /**
+     * @param pixmap Pixmap displayed in the header. The pixmap is by default right, \
but +     * @param alignment can be used to display it also left.
+     */
+    void setPixmap(const QPixmap &pixmap, ImageAlignment alignment = ImageRight);
+
+private:
+    class Private;
+    Private* const d;
+
+    Q_DISABLE_COPY(KTitleWidget)
+};
+
+#endif


[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