Added QWidgetList with proxy style to display stars as checkboxes
authorMateusz Półrola <mateusz.polrola@comarch.pl>
Tue, 17 Aug 2010 07:18:38 +0000 (09:18 +0200)
committerMateusz Półrola <mateusz.polrola@comarch.pl>
Tue, 17 Aug 2010 07:18:38 +0000 (09:18 +0200)
trunk/src/base/base.pro
trunk/src/base/gui/MainWindow.cpp
trunk/src/base/gui/WordListItem.cpp [deleted file]
trunk/src/base/gui/WordListItem.h [deleted file]
trunk/src/base/gui/WordListProxyStyle.cpp [new file with mode: 0644]
trunk/src/base/gui/WordListProxyStyle.h [new file with mode: 0644]
trunk/src/base/gui/WordListWidget.cpp
trunk/src/base/gui/WordListWidget.h
trunk/src/base/gui/gui.qrc [new file with mode: 0644]
trunk/src/base/gui/staroff.png [new file with mode: 0644]
trunk/src/base/gui/staron.png [new file with mode: 0644]

index 2faa1eb..07d62a1 100644 (file)
@@ -33,7 +33,7 @@ SOURCES += gui/main.cpp\
     gui/DictTypeSelectDialog.cpp \
     backbone/History.cpp \
     gui/HistoryListDialog.cpp \
-    gui/WordListItem.cpp
+    gui/WordListProxyStyle.cpp
 
 HEADERS  += gui/MainWindow.h \
     gui/SearchBarWidget.h \
@@ -51,7 +51,7 @@ HEADERS  += gui/MainWindow.h \
     ../includes/History.h \
     gui/HistoryListDialog.h \
     ../includes/GUIInterface.h \
-    gui/WordListItem.h
+    gui/WordListProxyStyle.h
 
 FORMS    += gui/MainWindow.ui
 
@@ -79,10 +79,13 @@ maemo5 {
   icon64.path = $$DATADIR/icons/hicolor/64x64/hildon
 }
 
-unix {
+!maemo5 {
   desktop.path = $$DATADIR/applications
   icon64.path = $$DATADIR/icons
 }
   desktop.files += ../../../data/other/$${TARGET}.desktop
   icon64.files += ../../../data/icons/64x64/$${TARGET}.png
 }
+
+RESOURCES += \
+    gui/gui.qrc
index 3d57b18..d4e98eb 100644 (file)
@@ -65,6 +65,7 @@ void MainWindow::initializeUI() {
 
 
     searchBarWidget = new SearchBarWidget;
+
     wordListWidget = new WordListWidget;
 
     //translationWidget is antoher stacked window, so we don't add it to layout
diff --git a/trunk/src/base/gui/WordListItem.cpp b/trunk/src/base/gui/WordListItem.cpp
deleted file mode 100644 (file)
index f2a7920..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-/*******************************************************************************
-
-    This file is part of mDictionary.
-
-    mDictionary 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 3 of the License, or
-    (at your option) any later version.
-
-    mDictionary 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 mDictionary.  If not, see <http://www.gnu.org/licenses/>.
-
-    Copyright 2010 Comarch S.A.
-
-*******************************************************************************/
-
-//Created by Mateusz Półrola
-
-#include "WordListItem.h"
-#include <QDebug>
-
-WordListItem::WordListItem(QString text, QWidget *parent) :
-    QWidget(parent)
-{
-    label = new QLabel(text);
-    button = new QToolButton;
-
-    layout = new QHBoxLayout;
-    setLayout(layout);
-
-    initializeUI();
-
-}
-
-
-
-void WordListItem::initializeUI() {
-
-
-    layout->addWidget(label);
-
-    #ifdef Q_WS_MAEMO_5
-        setMaximumHeight(75);
-        setMinimumHeight(75);
-    #else
-        setMaximumHeight(50);
-        setMinimumHeight(50);
-    #endif
-    _selected = false;
-
-
-    layout->addWidget(button,0, Qt::AlignRight);
-}
-
-void WordListItem::unselect() {
-    if(_selected) {
-        _selected = false;
-        update();
-    }
-}
-
-
-QString WordListItem::text() {
-    return label->text();
-}
-
-void WordListItem::mousePressEvent(QMouseEvent *e) {
-    if(e->button() == Qt::LeftButton) {
-        _selected = true;
-        update();
-        emit selected(label->text());
-    }
-}
-
-void WordListItem::mouseReleaseEvent(QMouseEvent *e) {
-    if(e->button() == Qt::LeftButton) {
-        if(e->y() != -1000)
-            emit clicked(label->text());
-    }
-}
-
-void WordListItem::paintEvent(QPaintEvent *e) {
-    QPainter p(this);
-
-    if(_selected) {
-        p.fillRect(e->rect(), palette().highlight());
-    }
-    else {
-        p.fillRect(e->rect(), palette().background());
-    }
-
-    //p.drawRect(e->rect());
-
-    QWidget::paintEvent(e);
-}
diff --git a/trunk/src/base/gui/WordListItem.h b/trunk/src/base/gui/WordListItem.h
deleted file mode 100644 (file)
index 460e1e1..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-/*******************************************************************************
-
-    This file is part of mDictionary.
-
-    mDictionary 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 3 of the License, or
-    (at your option) any later version.
-
-    mDictionary 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 mDictionary.  If not, see <http://www.gnu.org/licenses/>.
-
-    Copyright 2010 Comarch S.A.
-
-*******************************************************************************/
-
-//Created by Mateusz Półrola
-
-#ifndef WORDLISTITEM_H
-#define WORDLISTITEM_H
-
-#include <QWidget>
-#include <QtGui>
-
-class WordListItem : public QWidget
-{
-    Q_OBJECT
-public:
-    explicit WordListItem(QString text, QWidget *parent = 0);
-    void unselect();
-    QString text();
-
-Q_SIGNALS:
-    void clicked(QString text);
-    void selected(QString text);
-
-protected:
-    void mousePressEvent(QMouseEvent *);
-    void mouseReleaseEvent(QMouseEvent *);
-    void paintEvent(QPaintEvent *);
-
-private:
-    QLabel* label;
-    QIcon* icon;
-    QToolButton* button;
-    QHBoxLayout* layout;
-    bool _selected;
-
-    void initializeUI();
-};
-
-#endif // WORDLISTITEM_H
diff --git a/trunk/src/base/gui/WordListProxyStyle.cpp b/trunk/src/base/gui/WordListProxyStyle.cpp
new file mode 100644 (file)
index 0000000..57d03c4
--- /dev/null
@@ -0,0 +1,50 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary 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 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary 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 mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#include "WordListProxyStyle.h"
+
+
+WordListProxyStyle::WordListProxyStyle() :
+    QProxyStyle()
+{
+    starPixmapOn = QPixmap(":/icons/staron.png");
+    starPixmapOff = QPixmap(":/icons/staroff.png");
+}
+
+
+void WordListProxyStyle::drawPrimitive(PrimitiveElement element,
+                                       const QStyleOption *option,
+                                       QPainter *painter,
+                                       const QWidget *widget) const {
+    if(element == PE_IndicatorCheckBox) {
+        if(option->state & QStyle::State_On)
+            painter->drawPixmap(option->rect, starPixmapOn);
+        else
+            painter->drawPixmap(option->rect, starPixmapOff);
+    }
+    else {
+        QProxyStyle::drawPrimitive(element, option, painter, widget);
+    }
+}
+
+
diff --git a/trunk/src/base/gui/WordListProxyStyle.h b/trunk/src/base/gui/WordListProxyStyle.h
new file mode 100644 (file)
index 0000000..dd76e73
--- /dev/null
@@ -0,0 +1,44 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary 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 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary 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 mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#ifndef WORDLISTPROXYSTYLE_H
+#define WORDLISTPROXYSTYLE_H
+
+#include <QProxyStyle>
+#include <QtGui>
+
+class WordListProxyStyle : public QProxyStyle
+{
+public:
+    WordListProxyStyle();
+    void drawPrimitive(PrimitiveElement element,
+                       const QStyleOption *option,
+                       QPainter *painter,
+                       const QWidget *widget) const;
+
+private:
+    QPixmap starPixmapOn;
+    QPixmap starPixmapOff;
+};
+
+#endif // WORDLISTPROXYSTYLE_H
index 389c779..f8133ed 100644 (file)
@@ -25,6 +25,7 @@
 #include <QDebug>
 #include "../../includes/translation.h"
 #include <QMultiHash>
+#include "WordListProxyStyle.h"
 
 
 #ifdef Q_WS_MAEMO_5
 #endif
 
 WordListWidget::WordListWidget(QWidget *parent):
-    QScrollArea(parent) {
+    QListWidget(parent) {
 
+    connect(this, SIGNAL(itemPressed(QListWidgetItem*)),
+            this, SLOT(wordPressed(QListWidgetItem*)));
 
-    QWidget* w = new QWidget;
-    layout = new QVBoxLayout(w);
-    setWidget(w);
-    setWidgetResizable(true);
 
-    setMinimumWidth(300);
+    connect(this, SIGNAL(itemClicked(QListWidgetItem*)),
+            this, SLOT(wordClicked(QListWidgetItem*)));
 
-}
 
-void WordListWidget::addWord(QString word) {
-    WordListItem* w = new WordListItem(word);
-    layout->addWidget(w);
-    items.append(w);
+    setStyle(new WordListProxyStyle);
 
-    connect(w, SIGNAL(clicked(QString)),
-            this, SLOT(itemClicked(QString)));
+    setLayoutDirection(Qt::RightToLeft);
 
-    connect(w, SIGNAL(selected(QString)),
-            this, SLOT(itemSelected(QString)));
 }
 
-void WordListWidget::clear() {
-    int wordsCount = items.count();
-    for(int i = 0; i < wordsCount; i++) {
-        delete items.at(i);
-    }
-    items.clear();
+void WordListWidget::addWord(QString word) {
+    QListWidgetItem* item = new QListWidgetItem(word, this);
+    item->setCheckState(Qt::Unchecked);
+    item->setTextAlignment(Qt::AlignRight);
 }
 
+
 void WordListWidget::showSearchResults(
-       QHash<QString, QList<Translation *> > result) {
+        QHash<QString, QList<Translation *> > result) {
     clear();
     searchResult.clear();
 
@@ -74,18 +66,20 @@ void WordListWidget::showSearchResults(
     for(i = result.begin(); i != result.end(); i++) {
            addWord(i.key());
     }
-
 }
 
-void WordListWidget::itemClicked(QString key) {
-    emit showTranslation(searchResult[key]);
+void WordListWidget::wordClicked(QListWidgetItem *item) {
+    if(itemState == item->checkState()) {
+        emit showTranslation(
+                searchResult[item->data(Qt::DisplayRole).toString()]);
+    }
+    else {
+        qDebug()<<"edited";
+    }
 }
 
-void WordListWidget::itemSelected(QString key) {
-    for(int i = 0; i < items.count(); i++) {
-        if(items.at(i)->text() != key)
-            items.at(i)->unselect();
-    }
+void WordListWidget::wordPressed(QListWidgetItem *item) {
+    itemState = item->checkState();
 }
 
 void WordListWidget::lockList() {
index 6223e1d..3112f72 100644 (file)
 #include <QStringListModel>
 #include "../backbone/backbone.h"
 #include "SearchBarWidget.h"
-#include "WordListItem.h"
 
 //! Displays list of words found in dictionaries
 /*!
     It allow user to select word to see it's translation or to mark it as "star"
   */
-class WordListWidget : public QScrollArea {
+class WordListWidget : public QListWidget {
     Q_OBJECT
 public:
     explicit WordListWidget(QWidget *parent = 0);
@@ -60,23 +59,15 @@ public Q_SLOTS:
     //! Unlocks words list
     void unlockList();
 
-
 private Q_SLOTS:
-    void itemClicked(QString key);
-    void itemSelected(QString key);
+    void wordClicked(QListWidgetItem* item);
+    void wordPressed(QListWidgetItem* item);
 
 private:
-    //Backbone *backbone;
-    //words are keeping as QStringListModel which allow to sort them
-    //QStringListModel *wordListModel;
     void addWord(QString word);
-    //clears all list of words
-    void clear();
-    QHash<QString, QList<Translation*> > searchResult;
-    QList<WordListItem*> items;
 
-    QVBoxLayout* layout;
-    //QString _exactMatchString;
+    Qt::CheckState itemState;
+    QHash<QString, QList<Translation*> > searchResult;
 };
 
 #endif // WORDLISTWIDGET_H
diff --git a/trunk/src/base/gui/gui.qrc b/trunk/src/base/gui/gui.qrc
new file mode 100644 (file)
index 0000000..0bd0ca2
--- /dev/null
@@ -0,0 +1,6 @@
+<RCC>
+    <qresource prefix="/icons">
+        <file>staroff.png</file>
+        <file>staron.png</file>
+    </qresource>
+</RCC>
diff --git a/trunk/src/base/gui/staroff.png b/trunk/src/base/gui/staroff.png
new file mode 100644 (file)
index 0000000..7b0c1bc
Binary files /dev/null and b/trunk/src/base/gui/staroff.png differ
diff --git a/trunk/src/base/gui/staron.png b/trunk/src/base/gui/staron.png
new file mode 100644 (file)
index 0000000..2291707
Binary files /dev/null and b/trunk/src/base/gui/staron.png differ