Added custom word list
authorMateusz Półrola <mateusz.polrola@comarch.pl>
Mon, 16 Aug 2010 13:23:28 +0000 (15:23 +0200)
committerMateusz Półrola <mateusz.polrola@comarch.pl>
Mon, 16 Aug 2010 13:23:28 +0000 (15:23 +0200)
trunk/src/base/base.pro
trunk/src/base/gui/MainWindow.cpp
trunk/src/base/gui/WordListItem.cpp [new file with mode: 0644]
trunk/src/base/gui/WordListItem.h [new file with mode: 0644]
trunk/src/base/gui/WordListWidget.cpp
trunk/src/base/gui/WordListWidget.h

index 03bfb66..2faa1eb 100644 (file)
@@ -32,7 +32,8 @@ SOURCES += gui/main.cpp\
     gui/DictManagerWidget.cpp \
     gui/DictTypeSelectDialog.cpp \
     backbone/History.cpp \
-    gui/HistoryListDialog.cpp
+    gui/HistoryListDialog.cpp \
+    gui/WordListItem.cpp
 
 HEADERS  += gui/MainWindow.h \
     gui/SearchBarWidget.h \
@@ -49,7 +50,8 @@ HEADERS  += gui/MainWindow.h \
     gui/TranslationWidgetAutoResizer.h \
     ../includes/History.h \
     gui/HistoryListDialog.h \
-    ../includes/GUIInterface.h
+    ../includes/GUIInterface.h \
+    gui/WordListItem.h
 
 FORMS    += gui/MainWindow.ui
 
index 943cc78..3d57b18 100644 (file)
@@ -46,6 +46,8 @@ MainWindow::MainWindow(Backbone *backbone, QWidget *parent):
     setExactSearch(false);
 
     setWindowTitle("mDictionary");
+
+    showMaximized();
 }
 
 MainWindow::~MainWindow() {
diff --git a/trunk/src/base/gui/WordListItem.cpp b/trunk/src/base/gui/WordListItem.cpp
new file mode 100644 (file)
index 0000000..f2a7920
--- /dev/null
@@ -0,0 +1,100 @@
+/*******************************************************************************
+
+    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
new file mode 100644 (file)
index 0000000..460e1e1
--- /dev/null
@@ -0,0 +1,57 @@
+/*******************************************************************************
+
+    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
index bd3cf02..389c779 100644 (file)
 #include "../../includes/translation.h"
 #include <QMultiHash>
 
+
 #ifdef Q_WS_MAEMO_5
     #include <QMaemo5InformationBox>
 #endif
 
 WordListWidget::WordListWidget(QWidget *parent):
-    QListView(parent) {
+    QScrollArea(parent) {
 
-    wordListModel = new QStringListModel();
 
-    connect(this, SIGNAL(clicked(QModelIndex)),
-            this, SLOT(itemClicked(QModelIndex)));
+    QWidget* w = new QWidget;
+    layout = new QVBoxLayout(w);
+    setWidget(w);
+    setWidgetResizable(true);
 
+    setMinimumWidth(300);
 
-    setModel(wordListModel);
 }
 
 void WordListWidget::addWord(QString word) {
-    int wordsCount = wordListModel->rowCount();
-
-    wordListModel->insertRow(wordsCount);
+    WordListItem* w = new WordListItem(word);
+    layout->addWidget(w);
+    items.append(w);
 
-    QModelIndex newWordIndex = wordListModel->index(wordsCount);
-
-    wordListModel->setData(newWordIndex, word);
+    connect(w, SIGNAL(clicked(QString)),
+            this, SLOT(itemClicked(QString)));
 
+    connect(w, SIGNAL(selected(QString)),
+            this, SLOT(itemSelected(QString)));
 }
 
 void WordListWidget::clear() {
-    int wordsCount = wordListModel->rowCount();
-
+    int wordsCount = items.count();
     for(int i = 0; i < wordsCount; i++) {
-        wordListModel->removeRow(0);
+        delete items.at(i);
     }
+    items.clear();
 }
 
 void WordListWidget::showSearchResults(
@@ -72,14 +75,17 @@ void WordListWidget::showSearchResults(
            addWord(i.key());
     }
 
-    wordListModel->sort(0, Qt::AscendingOrder);
-
-    scrollTo(model()->index(0,0));
+}
 
+void WordListWidget::itemClicked(QString key) {
+    emit showTranslation(searchResult[key]);
 }
 
-void WordListWidget::itemClicked(QModelIndex index) {
-    emit showTranslation(searchResult[index.model()->data(index).toString()]);
+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::lockList() {
index d2cff2c..6223e1d 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 QListView {
+class WordListWidget : public QScrollArea {
     Q_OBJECT
 public:
     explicit WordListWidget(QWidget *parent = 0);
@@ -61,16 +62,20 @@ public Q_SLOTS:
 
 
 private Q_SLOTS:
-    void itemClicked(QModelIndex index);
+    void itemClicked(QString key);
+    void itemSelected(QString key);
 
 private:
     //Backbone *backbone;
     //words are keeping as QStringListModel which allow to sort them
-    QStringListModel *wordListModel;
+    //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;
 };