Initial impl. of adding.

This commit is contained in:
Martin Rotter 2014-01-18 07:39:43 +01:00
parent b8af2d26be
commit 1b99356a9b
11 changed files with 118 additions and 34 deletions

View File

@ -133,6 +133,23 @@ int FeedsModel::rowCount(const QModelIndex &parent) const {
return parent_item->childCount();
}
bool FeedsModel::addItem(FeedsModelRootItem *item,
FeedsModelRootItem *parent) {
QModelIndex parent_index = indexForItem(parent);
beginInsertRows(parent_index, parent->childCount(), parent->childCount());
// Add item to hierarchy.
parent->appendChild(item);
// Add item to persistent storage.
item->addItself();
endInsertRows();
return true;
}
bool FeedsModel::removeItem(const QModelIndex &index) {
if (index.isValid()) {
QModelIndex parent_index = index.parent();
@ -329,7 +346,6 @@ void FeedsModel::loadFromDatabase() {
}
case FeedsModelCategory::Feedly:
case FeedsModelCategory::TinyTinyRss:
default:
// NOTE: Not yet implemented.
break;

View File

@ -46,6 +46,8 @@ class FeedsModel : public QAbstractItemModel {
}
// Feed/category manipulators.
bool addItem(FeedsModelRootItem *item,
FeedsModelRootItem *parent);
bool removeItem(const QModelIndex &index);
// Returns (undeleted) messages for given feeds.

View File

@ -15,8 +15,7 @@ class FeedsModelCategory : public FeedsModelRootItem {
// NOTE: This is equivavelnt to Categories(type).
enum Type {
Standard = 0,
Feedly = 1,
TinyTinyRss = 2
Feedly = 1
};
// Constructors and destructors

View File

@ -57,6 +57,10 @@ class FeedsModelRootItem {
virtual int countOfUnreadMessages() const;
virtual int countOfAllMessages() const;
virtual bool addItself() {
return false;
}
// This method is used to permanently
// "remove" (or "unregister") this item.
// This typically removes item and its

View File

@ -91,6 +91,39 @@ QVariant FeedsModelStandardCategory::data(int column, int role) const {
}
}
bool FeedsModelStandardCategory::addItself() {
// Children are removed, remove this standard category too.
QSqlDatabase database = DatabaseFactory::instance()->connection();
QSqlQuery query_add(database);
query_add.setForwardOnly(true);
// Remove all messages from this standard feed.
query_add.prepare("INSERT INTO Categories "
"(parent_id, title, description, date_created, icon, type) "
"VALUES (:parent_id, :title, :description, :date_created, :icon, :type);");
query_add.bindValue(":parent_id", m_parentItem->id());
query_add.bindValue(":title", m_title);
query_add.bindValue(":description", m_description);
query_add.bindValue(":date_created", m_creationDate.toMSecsSinceEpoch());
query_add.bindValue(":icon", IconFactory::toByteArray(m_icon));
query_add.bindValue(":type", (int) m_type);
if (!query_add.exec()) {
return false;
}
query_add.prepare("SELECT id FROM Categories WHERE date_created = :date_created;");
query_add.bindValue(":date_created", m_creationDate.toMSecsSinceEpoch());
if (query_add.exec() && query_add.next()) {
setId(query_add.value(0).toInt());
return true;
}
else {
return false;
}
}
bool FeedsModelStandardCategory::removeItself() {
bool result = true;

View File

@ -21,6 +21,9 @@ class FeedsModelStandardCategory : public FeedsModelCategory {
// Returns the actual data representation of standard category.
QVariant data(int column, int role) const;
// Add itself to persistent database.
bool addItself();
// Removes category and all its children from persistent
// database.
bool removeItself();

View File

@ -49,7 +49,8 @@ bool FeedsProxyModel::lessThan(const QModelIndex &left,
else {
// In other cases, sort by title.
// TODO: mozna pouzit QString::localeAwareCompare tady.
return left_item->title() < right_item->title();
return QString::compare(left_item->title(),
right_item->title()) < 0;
}
}
else if (left_item->kind() == FeedsModelRootItem::Feed) {

View File

@ -32,10 +32,6 @@ FeedsView::~FeedsView() {
qDebug("Destroying FeedsView instance.");
}
void FeedsView::setSortingEnabled(bool enable) {
QTreeView::setSortingEnabled(enable);
header()->setSortIndicatorShown(false);
@ -75,7 +71,7 @@ void FeedsView::clearSelectedFeeds() {
void FeedsView::addNewCategory() {
QPointer<FormCategoryDetails> form_pointer = new FormCategoryDetails(m_sourceModel, this);
FormCategoryDetailsAnswer answer = form_pointer.data()->exec(NULL, NULL);
FormCategoryDetailsAnswer answer = form_pointer.data()->exec(NULL);
if (answer.m_dialogCode == QDialog::Accepted) {
// User submitted some new category and
@ -104,7 +100,19 @@ void FeedsView::editSelectedItem() {
}
void FeedsView::deleteSelectedItem() {
m_sourceModel->removeItem(m_proxyModel->mapToSource(currentIndex()));
QModelIndex current_index = currentIndex();
QItemSelectionModel *selection_model = selectionModel();
if (!current_index.isValid()) {
return;
}
if (selection_model->selectedRows().size() > 1) {
selection_model->clearSelection();
selection_model->select(current_index, QItemSelectionModel::Rows | QItemSelectionModel::SelectCurrent);
}
m_sourceModel->removeItem(m_proxyModel->mapToSource(current_index));
}
void FeedsView::markSelectedFeedsReadStatus(int read) {
@ -218,7 +226,7 @@ void FeedsView::setupAppearance() {
setIndentation(10);
setDragDropMode(QAbstractItemView::NoDragDrop);
setAllColumnsShowFocus(true);
setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionMode(QAbstractItemView::ExtendedSelection);
setRootIsDecorated(false);
// Sort in ascending order, that is categories are

View File

@ -3,22 +3,28 @@
#include "core/defs.h"
#include "core/feedsmodelrootitem.h"
#include "core/feedsmodelcategory.h"
#include "core/feedsmodelstandardcategory.h"
#include "core/feedsmodel.h"
#include "gui/iconthemefactory.h"
#include "gui/feedsview.h"
#include <QLineEdit>
#include <QTextEdit>
#include <QDialogButtonBox>
#include <QToolButton>
#include "QComboBox"
FormCategoryDetails::FormCategoryDetails(FeedsModel *model, QWidget *parent)
: QDialog(parent), m_editableCategory(NULL) {
: QDialog(parent),
m_editableCategory(NULL),
m_feedsModel(model) {
initialize();
loadCategories(model->allCategories().values(),
model->rootItem());
setWindowTitle(tr("Add new category"));
connect(m_ui->m_buttonBox, SIGNAL(accepted()),
this, SLOT(apply()));
}
FormCategoryDetails::~FormCategoryDetails() {
@ -35,15 +41,16 @@ void FormCategoryDetails::setEditableCategory(FeedsModelCategory *editable_categ
m_ui->m_btnIcon->setIcon(editable_category->icon());
}
FormCategoryDetailsAnswer FormCategoryDetails::exec(FeedsModelCategory *input_category,
FeedsModelCategory *input_parent_category) {
FormCategoryDetailsAnswer FormCategoryDetails::exec(FeedsModelCategory *input_category) {
FormCategoryDetailsAnswer answer;
if (input_category == NULL) {
// User is adding new category.
setWindowTitle(tr("Add new category"));
}
else {
// User is editing existing category.
setWindowTitle(tr("Edit existing category"));
setEditableCategory(input_category);
}
@ -52,6 +59,30 @@ FormCategoryDetailsAnswer FormCategoryDetails::exec(FeedsModelCategory *input_ca
return answer;
}
void FormCategoryDetails::apply() {
if (m_editableCategory == NULL) {
// add category
FeedsModelRootItem *parent = static_cast<FeedsModelRootItem*>(m_ui->m_cmbParentCategory->itemData(m_ui->m_cmbParentCategory->currentIndex()).value<void*>());
FeedsModelStandardCategory *cat = new FeedsModelStandardCategory();
cat->setTitle(m_ui->m_txtTitle->text());
cat->setCreationDate(QDateTime::currentDateTime());
cat->setDescription(m_ui->m_txtDescription->toPlainText());
cat->setIcon(m_ui->m_btnIcon->icon());
cat->setType(FeedsModelCategory::Standard);
if (m_feedsModel->addItem(cat, parent)) {
accept();
}
else {
// TODO: hlasit chybu
}
}
else {
// edit category
}
}
void FormCategoryDetails::initialize() {
m_ui = new Ui::FormCategoryDetails();
m_ui->setupUi(this);
@ -74,6 +105,6 @@ void FormCategoryDetails::loadCategories(const QList<FeedsModelCategory *> categ
m_ui->m_cmbParentCategory->addItem(category->data(FDS_MODEL_TITLE_INDEX,
Qt::DecorationRole).value<QIcon>(),
category->title(),
category->id());
QVariant::fromValue((void*) category));
}
}

View File

@ -41,8 +41,10 @@ class FormCategoryDetails : public QDialog {
// to the database.
// NOTE: Newly EDITED category IS COPY of its original.
// SO NO ORIGINAL MODEL DATA ARE EDITED OR CHANGED.
FormCategoryDetailsAnswer exec(FeedsModelCategory *input_category,
FeedsModelCategory *input_parent_category);
FormCategoryDetailsAnswer exec(FeedsModelCategory *input_category);
protected slots:
void apply();
protected:
// Sets the category which will be edited.
@ -59,6 +61,7 @@ class FormCategoryDetails : public QDialog {
private:
Ui::FormCategoryDetails *m_ui;
FeedsModelCategory *m_editableCategory;
FeedsModel *m_feedsModel;
};
#endif // FORMCATEGORYDETAILS_H

View File

@ -108,22 +108,6 @@
</widget>
<resources/>
<connections>
<connection>
<sender>m_buttonBox</sender>
<signal>accepted()</signal>
<receiver>FormCategoryDetails</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_buttonBox</sender>
<signal>rejected()</signal>