void DictManagerModel::clear()
{
- beginRemoveRows(QModelIndex(), 0, rowCount());
+ bool empty = true;
+ if (!_dictionaries.isEmpty()){
+// beginRemoveRows(QModelIndex(), 0, rowCount());
+ beginResetModel();
+ empty = false;
+ }
_dictionaries.clear();
_dictList.clear();
- endRemoveRows();
- Q_EMIT dataChanged(QModelIndex(), QModelIndex());
+
+ if (!empty){
+ endResetModel();
+ }
}
QVariant DictManagerModel::data(const QModelIndex & index, int role) const
if (value.type() == QVariant::Bool)
{
_dictionaries[dictionary] = value.toBool();
-// if (index == _dictList.count())
-// emit dataChanged(this->index(index-1), this->index(index));
Q_EMIT dataChanged(this->index(0), this->index(_dictList.count() - 1));
return 2;
}
Qt::ItemFlags DictManagerModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags fl = QAbstractItemModel::flags(index);
- qDebug("lol1");
return (fl | Qt::ItemIsEditable);
}
#include "DictManagerModel.h"
#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeContext>
-#include <QDebug>
/*!
--- /dev/null
+#include "WordListModel.h"
+
+WordListModel::WordListModel(/*QHash<QString, QList<Translation *> > translations, QHash<QString, bool> wordsInBookmarks, */QObject *parent) :
+ QAbstractListModel(parent)
+{
+ QHash<int, QByteArray> roles;
+ roles[WordRole] = "word";
+ roles[IsBookmarkedRole] = "isBookmarked";
+ roles[NumberRole] = "number";
+ setRoleNames(roles);
+
+ //setTranslations(translations, wordsInBookmarks);
+
+// connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(itemChanged()));
+}
+
+int WordListModel::rowCount(const QModelIndex &parent) const
+{
+ return _wordList.count();
+}
+
+void WordListModel::addWord(QString word, QList<Translation *> translations, bool isBookmarked)
+{
+ beginInsertRows(QModelIndex(), rowCount(), rowCount());
+ _translations.insert(word, translations);
+ _wordList << word;
+ _wordInBookmarks.insert(word, isBookmarked);
+ endInsertRows();
+}
+
+void WordListModel::clear()
+{
+ bool empty = true;
+ if (!_wordList.isEmpty()){
+ beginResetModel();
+ empty = false;
+ }
+ _translations.clear();
+ _wordInBookmarks.clear();
+ _wordList.clear();
+ if (!empty){
+ endResetModel();
+ }
+}
+
+QVariant WordListModel::data(const QModelIndex &index, int role) const
+{
+ if (index.row() < 0 || index.row() > _translations.count()){
+ return QVariant();
+ }
+
+ if (role == WordRole)
+ {
+ return _wordList[index.row()];
+ }
+ if (role == NumberRole){
+ return index.row();
+ }
+ if (role == IsBookmarkedRole){
+ return _wordInBookmarks[_wordList[index.row()]];
+ }
+ return QVariant();
+}
+
+bool WordListModel::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+ int res = setDataPriv(index.row(), value, role);
+ if (res == 0)
+ return false;
+ if (res > 0)
+ return true;
+ return true;
+}
+
+Qt::ItemFlags WordListModel::flags(const QModelIndex &index) const
+{
+ Qt::ItemFlags fl = QAbstractItemModel::flags(index);
+ return (fl | Qt::ItemIsEditable);
+}
+
+void WordListModel::setTranslations(QHash<QString, QList<Translation *> > translations, QHash<QString, bool> wordsInBookmarks)
+{
+ QHashIterator<QString, QList<Translation *> > i(translations);
+ while (i.hasNext()) {
+ i.next();
+ addWord(i.key(), i.value(), wordsInBookmarks[i.key()]);
+ }
+}
+
+void WordListModel::setModelProperty(int index, const QVariant value, QString role)
+{
+ if (role.contains("isBookmarked"))
+ {
+ setDataPriv(index, value, IsBookmarkedRole);
+ }
+}
+
+int WordListModel::setDataPriv(int index, const QVariant &value, int role)
+{
+ if (index < 0 || index > _translations.count())
+ return 0;
+
+ QString word = _wordList[index];
+ if (role == WordRole)
+ return 1;
+ if (role == NumberRole)
+ return 1;
+ if (role == IsBookmarkedRole)
+ {
+ if (value.type() == QVariant::Bool)
+ {
+ _wordInBookmarks[word] = value.toBool();
+ Q_EMIT dataChanged(this->index(0), this->index(_translations.count() - 1));
+ if (_wordInBookmarks[word] == true){
+ Q_EMIT addToBookmarks(word);
+ } else {
+ Q_EMIT removeFromBookmarks(word);
+ }
+ return 2;
+ }
+ else
+ {
+ return 0;
+ }
+ }
+ return 0;
+}
--- /dev/null
+#ifndef WORDLISTMODEL_H
+#define WORDLISTMODEL_H
+
+#include <QAbstractListModel>
+//#include "../../include/GUIInterface.h"
+#include "../../include/translation.h"
+
+class WordListModel : public QAbstractListModel
+{
+ Q_OBJECT
+public:
+
+ enum DictTypeRoles
+ {
+ WordRole = Qt::UserRole + 1,
+ IsBookmarkedRole,
+ NumberRole
+ };
+
+ explicit WordListModel(/*QHash<QString, QList<Translation*> > translations, QHash<QString, bool> wordsInBookmarks, */QObject *parent = 0);
+
+ int rowCount(const QModelIndex & parent = QModelIndex()) const;
+
+ QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
+ bool setData(const QModelIndex &index, const QVariant &value, int role);
+
+ Qt::ItemFlags flags(const QModelIndex &index) const;
+
+ //! Replace model data and refresh ui.
+ /*!
+ \param dictionaries hash set (dictionary, is active) with dictionaries
+ */
+ void setTranslations(QHash<QString, QList<Translation*> > translations, QHash<QString, bool> wordsInBookmarks);
+ //! Clear model data and refresh UI
+ void clear();
+
+signals:
+
+// //! Set index of current selected word
+// /*!
+// \param index word position in data list
+// */
+// void itemSelected(int index);
+
+ void addToBookmarks(QString word);
+
+ void removeFromBookmarks(QString word);
+
+public slots:
+
+ //! Set value at role in index row of data.
+ /*!
+ \param index word position in data list
+ \param value new value for role
+ \param role role name
+ */
+ void setModelProperty(int index, const QVariant value, QString role);
+
+private:
+ int setDataPriv(int index, const QVariant &value, int role);
+ void addWord(QString word, QList<Translation*> translations, bool isBookmarked);
+
+ QHash<QString, QList<Translation*> > _translations;
+ QHash<QString, bool > _wordInBookmarks;
+ QList<QString> _wordList;
+
+};
+
+#endif // WORDLISTMODEL_H
QTreeView(parent) {
//creating new model to store words and stars
+#ifdef Q_WS_MAEMO_5
model = new QStandardItemModel(this);
setModel(model);
- setHeaderHidden(true);
- setRootIsDecorated(false);
+
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
//set our custom style to draw checkboxes as stars
proxyStyle = new WordListProxyStyle();
setStyle(proxyStyle);
+ //for future use, set checkbox icons for maemo
+// ctxt->setContextProperty("CheckedPath", "qrc:/icons/96x96/staron.png");
+// ctxt->setContextProperty("UncheckedPath", "qrc:/icons/96x96/staroff.png");
+#else
+
+ listModel = new WordListModel(this);
+
+ verticalLayout = new QVBoxLayout;
+ setLayout(verticalLayout);
+
+ qmlView = new QDeclarativeView(this);
+
+ ctxt = qmlView->rootContext();
+
+// refreshDictsList();
+ ctxt->setContextProperty("wordModel", &(*listModel));
+ ctxt->setContextProperty("CheckedPath", "qrc:/icons/16x16/staron.png");
+ ctxt->setContextProperty("UncheckedPath", "qrc:/icons/16x16/staroff.png");
+
+ qmlView->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/WordListWidget.qml"));
+
+ QGraphicsObject *rootObject = qmlView->rootObject();
+
+ qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
+
+ verticalLayout->addWidget(qmlView);
+
+ connect(rootObject, SIGNAL(wordSelected(QString)), this, SLOT(wordClicked(QString)));
+ connect(listModel, SIGNAL(addToBookmarks(QString)), this, SLOT(addToBookmarks(QString)));
+ connect(listModel, SIGNAL(removeFromBookmarks(QString)), this, SLOT(removeFromBookmarks(QString)));
+
+ connect(this, SIGNAL(setWordListState(QVariant)), rootObject, SLOT(setEnabled(QVariant)));
+
+#endif
+
+ setHeaderHidden(true);
+ setRootIsDecorated(false);
+
//setting size of star in pixels, on maemo checboxes are much bigger
#ifdef Q_WS_MAEMO_5
checkBoxWidth = 70;
clear();
searchResult = result;
+#ifdef Q_WS_MAEMO
if(searchResult.count()>0) {
setEnabled(true);
model->setColumnCount(2);
model->setItem(0,item);
}
+#else
+
+ QHash<QString, bool> wordsInBookmarks;
+ QHashIterator<QString, QList<Translation *> > i(result);
+ while (i.hasNext()){
+ i.next();
+
+ bool bookmark = false;
+ Translation* t;
+ foreach(t, searchResult[i.key()]) {
+ if(t->isBookmark()) {
+ bookmark = true;
+ break;
+ }
+ }
+ wordsInBookmarks.insert(i.key(), bookmark);
+ }
+
+ if (result.count() == 0){
+ result.insert("!@#$%", QList<Translation*>());
+ wordsInBookmarks.insert("!@#$%", false);
+ Q_EMIT setWordListState(false);
+ }
+
+ if (listModel == 0){
+ listModel = new WordListModel(this);
+ }
+ listModel->setTranslations(result, wordsInBookmarks);
+ //todo: sort words
+
+#endif
setFocus();
}
+#ifdef Q_WS_MAEMO
void WordListWidget::wordClicked(QModelIndex index) {
//we're getting translation based on data in index
Q_EMIT showTranslation(
}
}
}
+#else
+ void WordListWidget::wordClicked(QString word){
+ emit showTranslation(searchResult[word]);
+ }
+
+ void WordListWidget::addToBookmarks(QString word){
+ emit addBookmark(searchResult[word]);
+ }
+
+ void WordListWidget::removeFromBookmarks(QString word){
+ emit removeBookmark(searchResult[word]);
+ }
+
+#endif
+#ifdef Q_WS_MAEMO_5
void WordListWidget::mouseReleaseEvent(QMouseEvent *event) {
//firstly we normally handle this event
QTreeView::resizeEvent(event);
}
-void WordListWidget::resizeColumns() {
- setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
- setColumnWidth(1, checkBoxWidth);
-}
-
void WordListWidget::keyPressEvent(QKeyEvent *event) {
QTreeView::keyPressEvent(event);
wordClicked(selectedIndexes().at(0));
}
}
+#endif
+
+void WordListWidget::resizeColumns() {
+ setColumnWidth(0, viewport()->width() -checkBoxWidth - 5);
+ setColumnWidth(1, checkBoxWidth);
+}
void WordListWidget::lockList() {
+#ifdef Q_WS_MAEMO_5
setEnabled(false);
+#else
+ Q_EMIT setWordListState(false);
+#endif
}
void WordListWidget::unlockList() {
+#ifdef Q_WS_MAEMO_5
setEnabled(true);
+#else
+ Q_EMIT setWordListState(false);
+#endif
}
void WordListWidget::clear() {
+#ifdef Q_WS_MAEMO_5
model->clear();
+#else
+ if (listModel != 0){
+ listModel->clear();
+ }
+#endif
QHash<QString, QList<Translation*> >::iterator i;
for(i = searchResult.begin(); i != searchResult.end(); i++) {
#include <QtGui>
#include <QStringListModel>
+#include <QVBoxLayout>
#include "../backbone/backbone.h"
#include "WordListProxyStyle.h"
+#include "WordListModel.h"
+#include <QtDeclarative/QDeclarativeView>
+#include <QtDeclarative/QDeclarativeContext>
/*!
It allows user to select word to see its translation or to mark or unmark
//! Requests to remove selected word from bookmarks
void removeBookmark(QList<Translation*>);
+ void setWordListState(QVariant state);
+
public Q_SLOTS:
//! Shows search results
void clear();
protected:
+#ifdef Q_WS_MAEMO_5
//! Reimplemented standard mouseReleaseEvent to check if user clicked on
//! a word or on its star to emit suitable signal
void mouseReleaseEvent(QMouseEvent *event);
//! Checks if user press return and if so displays translation of selected word
void keyPressEvent( QKeyEvent * event);
-
+#endif
private Q_SLOTS:
+#ifdef Q_WS_MAEMO_5
//! Emits signal to show translation of clicked item. Signal is emitted
//! only when a word was clicked.
void wordClicked(QModelIndex index);
//! Emits signal to show add or remove word from bookmarks.
//! Signal is emitted only when a star was clicked.
void wordChecked(QModelIndex index);
-
+#else
+ void wordClicked(QString word);
+ void addToBookmarks(QString word);
+ void removeFromBookmarks(QString word);
+#endif
private:
//! Association between words and their translations
QHash<QString, QList<Translation*> > searchResult;
WordListProxyStyle* proxyStyle;
+
+#ifndef Q_WS_MAEMO_5
+ QVBoxLayout* verticalLayout;
+ QDeclarativeView* qmlView;
+ QDeclarativeContext* ctxt;
+ WordListModel* listModel;
+#endif
};
#endif // WORDLISTWIDGET_H
gui/NotifyManager.cpp \
gui/SpinBox.cpp \
gui/DictTypeModel.cpp \
- gui/DictManagerModel.cpp
+ gui/DictManagerModel.cpp \
+ gui/WordListModel.cpp
HEADERS += gui/MainWindow.h \
backbone/ConfigGenerator.h \
gui/NotifyManager.h \
gui/SpinBox.h \
gui/DictTypeModel.h \
- gui/DictManagerModel.h
+ gui/DictManagerModel.h \
+ gui/WordListModel.h
RESOURCES += ../../data/gui.qrc
Image {
id: checkbox
property bool selected
+ property string pathToCheckedImage: "qrc:/button/checkboxChecked.png"
+ property string pathToUncheckedImage: "qrc:/button/checkbox.png"
signal changed
height: {
var aspectRatio = sourceSize.height / sourceSize.width
name: "checked";
when: (checkbox.selected == true);
- PropertyChanges { target: checkbox; source: "qrc:/button/checkboxChecked.png" }
+ PropertyChanges { target: checkbox; source: pathToCheckedImage }
},
State {
name: "unchecked";
when: (checkbox.selected == false);
- PropertyChanges { target: checkbox; source: "qrc:/button/checkbox.png" }
+ PropertyChanges { target: checkbox; source: pathToUncheckedImage }
}
]
MouseArea{
anchors.verticalCenter: parent.verticalCenter
color: "white"
font.bold: true
- text: (value>-1) ? (Math.floor((value - minimum) / (maximum - minimum) * 100) + '%') : ("???");
+ text: (value>-1) ? (Math.floor((value - minimum) / (maximum - minimum) * 100) + '%') : ("");
}
}
import Qt 4.7
Rectangle {
- width: 100
- height: 62
+
+ function changeWordState(nr, state) {
+ wordList.currentIndex = nr
+ wordModel.setModelProperty(wordList.currentIndex, state, "isBookmarked")
+
+ }
+
+// function setEnabled(Boolean) { wordList.enabled = Boolean; println(wordList.enabled) } // slot
+
+ signal wordSelected(string word);
+ //?
+// signal addToBookmarks(int nr);
+// signal removeFromBookmarks(int nr);
+
+ SystemPalette { id: myPalette; colorGroup: SystemPalette.Active }
+
+ id: rectangle1
+ color: myPalette.base
+ anchors.fill: parent
+
+ ElementsListView{
+ id: wordList
+ width: rectangle1.width
+// height: rectangle1.height
+ anchors.fill: parent
+ highlightResizeSpeed: 1000
+
+ delegate: Component{
+ id: wordListDelegate
+ Item {
+ width: rectangle1.width
+ height: {
+ if (wordText.height + 4 > check.height)
+ return wordText.height + 4;
+ else
+ return check.height;
+ }
+ Row {
+ anchors.fill: parent
+
+ Text {
+ id: wordText
+ text:
+ {
+ if (word == "!@#$%"){
+ qsTr("Can't find any matching words")
+ } else {
+ word
+ }
+ }
+
+ MouseArea{
+ anchors.fill: parent
+ onClicked: {
+ wordList.currentIndex = number
+ console.log("lolol")
+ rectangle1.wordSelected(word)
+ }
+ }
+ }
+
+ Checkbox{
+ id: check
+ width: wordText.height
+ selected: isBookmarked
+ pathToCheckedImage: CheckedPath
+ pathToUncheckedImage: UncheckedPath
+ anchors.leftMargin: 5
+ anchors.verticalCenter: parent.verticalCenter
+ onChanged: rectangle1.changeWordState(number, selected)
+ visible: {
+ if (word == "!@#$%"){
+ false
+ } else {
+ true
+ }
+ }
+ }
+
+ }
+ }
+
+ }
+
+ model: wordModel
+ }
}