GUI: attachments: change page storage from list to hash

Each page has an ID when it's stored in the model. This ID used to be
the position in the list. This was fine as long as the list wasn't
dynamic. However, with the new attachment editing features the user can
add and remove attachments in arbitrary order. This means that the
position inside the list changes but not the ID associated with the
page.

Therefore a hash is now used instead with a sequential number as the ID.
This commit is contained in:
Moritz Bunkus 2016-01-02 16:35:22 +01:00
parent 0bc231f4a2
commit 5109ce6127
3 changed files with 33 additions and 20 deletions

View File

@ -21,10 +21,11 @@ PageBase *
PageModel::selectedPage(QModelIndex const &idx)
const {
auto selectedItem = itemFromIndex(idx.sibling(idx.row(), 0));
if (selectedItem)
return m_pages[ selectedItem->data(Util::HeaderEditorPageIdRole).value<unsigned int>() ];
if (!selectedItem)
return {};
return nullptr;
auto pageId = selectedItem->data(Util::HeaderEditorPageIdRole).value<int>();
return m_pages.value(pageId, nullptr);
}
void
@ -32,43 +33,54 @@ PageModel::appendPage(PageBase *page,
QModelIndex const &parentIdx) {
page->retranslateUi();
auto pageId = ++m_pageId;
auto parentItem = parentIdx.isValid() ? itemFromIndex(parentIdx.sibling(parentIdx.row(), 0)) : invisibleRootItem();
auto newItems = QList<QStandardItem *>{};
for (auto idx = columnCount(); idx > 0; --idx)
newItems << new QStandardItem{};
newItems[0]->setData(static_cast<unsigned int>(m_pages.count()), Util::HeaderEditorPageIdRole);
newItems[0]->setData(pageId, Util::HeaderEditorPageIdRole);
parentItem->appendRow(newItems);
page->m_pageIdx = indexFromItem(newItems[0]);
page->setItems(newItems);
m_pages << page;
m_pages[pageId] = page;
if (!parentIdx.isValid())
m_topLevelPages << page;
}
bool
PageModel::deletePage(PageBase *page) {
auto idx = m_pages.indexOf(page);
if (idx == -1)
auto pageId = m_pages.key(page);
if (!pageId)
return false;
m_pages.removeAt(idx);
m_pages.remove(pageId);
delete page;
auto idx = m_topLevelPages.indexOf(page);
if (-1 != idx)
m_topLevelPages.removeAt(idx);
return true;
}
QList<PageBase *> &
PageModel::pages() {
return m_pages;
QList<PageBase *>
PageModel::pages()
const {
auto pages = QList<PageBase *>{};
for (auto const &page : m_pages)
pages << page;
return pages;
}
QList<PageBase *> &
PageModel::topLevelPages() {
QList<PageBase *> const &
PageModel::topLevelPages()
const {
return m_topLevelPages;
}

View File

@ -15,7 +15,9 @@ class PageBase;
class PageModel: public QStandardItemModel {
Q_OBJECT;
protected:
QList<PageBase *> m_pages, m_topLevelPages;
QHash<int, PageBase *> m_pages;
QList<PageBase *> m_topLevelPages;
int m_pageId{};
public:
PageModel(QObject *parent);
@ -26,8 +28,8 @@ public:
void appendPage(PageBase *page, QModelIndex const &parentIdx = {});
bool deletePage(PageBase *page);
QList<PageBase *> &pages();
QList<PageBase *> &topLevelPages();
QList<PageBase *> pages() const;
QList<PageBase *> const &topLevelPages() const;
void reset();

View File

@ -276,8 +276,7 @@ Tab::retranslateUi() {
m_saveAttachmentContentAction->setIcon(QIcon{Q(":/icons/16x16/document-save.png")});
m_replaceAttachmentContentAction->setIcon(QIcon{Q(":/icons/16x16/document-open.png")});
auto &pages = m_model->pages();
for (auto const &page : pages)
for (auto const &page : m_model->pages())
page->retranslateUi();
m_model->retranslateUi();
@ -320,7 +319,7 @@ Tab::title()
bool
Tab::hasBeenModified() {
auto pages = m_model->topLevelPages();
auto &pages = m_model->topLevelPages();
for (auto const &page : pages)
if (page->hasBeenModified())
return true;
@ -330,7 +329,7 @@ Tab::hasBeenModified() {
void
Tab::doModifications() {
auto pages = m_model->topLevelPages();
auto &pages = m_model->topLevelPages();
for (auto const &page : pages)
page->doModifications();