detect a disconect error in GooglePlugin
[mdictionary] / trunk / src / base / gui / WordListWidget.cpp
index df8902c..59f4724 100644 (file)
 WordListWidget::WordListWidget(QWidget *parent):
     QTreeView(parent) {
 
+    //creating new model to store words and stars
     model = new QStandardItemModel(this);
     setModel(model);
     setHeaderHidden(true);
     setRootIsDecorated(false);
     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
+    //set our custom style to draw checkboxes as stars
     setStyle(new WordListProxyStyle);
 
+    //setting size of star in pixels, on maemo checboxes are much bigger
     #ifdef Q_WS_MAEMO_5
         checkBoxWidth = 70;
     #else
@@ -53,11 +56,18 @@ WordListWidget::WordListWidget(QWidget *parent):
 
 void WordListWidget::addWord(QString word, int row) {
     QStandardItem* item = new QStandardItem(word);
+
+    //we don't want to allow user to edit word
     item->setFlags(item->flags() ^ Qt::ItemIsEditable);
+
     QStandardItem* itemCheckBox = new QStandardItem();
-    itemCheckBox->setFlags(itemCheckBox->flags() ^ Qt::ItemIsEditable |
+    //creating checkbox item
+    itemCheckBox->setFlags((itemCheckBox->flags() ^ Qt::ItemIsEditable) |
                            Qt::ItemIsUserCheckable);
 
+    /*checking if word is already in bookmarks, information about that is
+    stored in its translation object (not all translations have to be in
+    bookmarks)*/
     bool bookmark = false;
     Translation* t;
     foreach(t, searchResult[word]) {
@@ -72,6 +82,7 @@ void WordListWidget::addWord(QString word, int row) {
     else
         itemCheckBox->setCheckState(Qt::Unchecked);
 
+    //add item to model
     model->setItem(row,0, item);
     model->setItem(row,1, itemCheckBox);
 }
@@ -79,16 +90,18 @@ void WordListWidget::addWord(QString word, int row) {
 
 void WordListWidget::showSearchResults(
         QHash<QString, QList<Translation *> > result) {
-    model->clear();
-    searchResult.clear();
+
+    clear();
+    qDebug()<<searchResult.count();
+    searchResult = result;
+
 
     model->setColumnCount(2);
     model->setRowCount(result.count());
 
-    searchResult = result;
     int row=0;
     QHash<QString, QList<Translation*> >::iterator i;
-    for(i = result.begin(); i != result.end(); i++) {
+    for(i = searchResult.begin(); i != searchResult.end(); i++) {
            addWord(i.key(), row++);
     }
 
@@ -97,42 +110,62 @@ void WordListWidget::showSearchResults(
 }
 
 void WordListWidget::wordClicked(QModelIndex index) {
-    emit showTranslation(
+    //we're getting translation based on data in index
+    //qDebug()<<searchResult[index.data().toString()].at(0)->dictionaryInfo();
+    Q_EMIT showTranslation(
             searchResult[index.data().toString()]);
 }
 
 void WordListWidget::wordChecked(QModelIndex index) {
+
+    //save new item state
     Qt::CheckState state =
             Qt::CheckState(index.data(Qt::CheckStateRole).toInt());
 
-    if(selectedIndexes().count()==0) return;
+
+
+    //getting index of item which contains word which should be added/removed
+    //from bookmarks
     QModelIndex item = selectedIndexes().at(0);
     if(!item.isValid()) return;
 
+    //to shorten lag between clicking on star and its change
+    repaint();
+
+    //depending on new state emit suitable signal
     if(state == Qt::Checked) {
-        emit addBookmark(searchResult[item.data().toString()]);
+        Q_EMIT addBookmark(searchResult[item.data().toString()]);
     }
     else {
-        emit removeBookmark(searchResult[item.data().toString()]);
+        Q_EMIT removeBookmark(searchResult[item.data().toString()]);
     }
 }
 
 
 void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
-    QTreeView::mouseReleaseEvent(event);
 
+    //firstly we normally handle this event
+    QTreeView::mouseReleaseEvent(event);
 
+    //then we check at which item user clicked
     QModelIndex index = indexAt(event->pos());
     if(!index.isValid()) return;
+
+    /*if there are no selected items we return, that occurs sometimes
+    on maemo, when user is scrolling list and clicks to stop the scroll,
+    system doesn't select item but emits mouseReleaseEvent*/
     if(selectedIndexes().count() == 0) return;
 
+    //if user doesn't click either on word or on star, return
     if(selectedIndexes().at(0) != index && selectedIndexes().at(1) != index)
         return;
 
     int c = index.column();
     if(c==0)
+        //if column is 0 user clicked word
         wordClicked(index);
     else
+        //else user clicked star
         wordChecked(index);
 }
 
@@ -142,7 +175,7 @@ void WordListWidget::resizeEvent(QResizeEvent *event) {
 }
 
 void WordListWidget::resizeColumns() {
-    setColumnWidth(0, viewport()->width() -checkBoxWidth - 20);
+    setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
     setColumnWidth(1, checkBoxWidth);
 }
 
@@ -153,3 +186,16 @@ void WordListWidget::lockList() {
 void WordListWidget::unlockList() {
     setEnabled(true);
 }
+
+void WordListWidget::clear() {
+    model->clear();
+
+    QHash<QString, QList<Translation*> >::iterator i;
+    for(i = searchResult.begin(); i != searchResult.end(); i++) {
+           Translation*t;
+           foreach(t, i.value()) {
+               delete t;
+           }
+    }
+    searchResult.clear();
+}