Adds possibility to remove section contents.

This commit is contained in:
mfreiholz 2016-03-14 15:12:34 +01:00
parent 9c958d0d22
commit d2d2467101
15 changed files with 381 additions and 6 deletions

14
.astylerc Normal file
View File

@ -0,0 +1,14 @@
--style=allman
--indent=force-tab=4
--align-pointer=type
--align-reference=type
--pad-oper
--pad-header
--unpad-paren
--remove-comment-prefix
--mode=c

View File

@ -54,6 +54,13 @@ public:
*/
SectionWidget* addSectionContent(const SectionContent::RefPtr& sc, SectionWidget* sw = NULL, DropArea area = CenterDropArea);
/*!
* Completely removes the <em>sc</em> from this ContainerWidget.
* This container will no longer hold a reference to the content.
* The content can be safely deleted.
*/
bool removeSectionContent(const SectionContent::RefPtr& sc);
/*!
* Shows the specific SectionContent in UI.
* Independed of the current state, whether it is used inside a section or is floating.
@ -101,6 +108,12 @@ public:
QRect outerBottomDropRect() const;
QRect outerLeftDropRect() const;
/*!
* \brief contents
* \return List of known SectionContent for this ContainerWidget.
*/
QList<SectionContent::RefPtr> contents() const;
private:
//
// Internal Stuff Begins Here

View File

@ -57,6 +57,9 @@ private:
// Optional attributes
QString _title;
/* Note: This method could be a problem in static build environment
* since it may begin with 0 for every module which uses ADS.
*/
static int GetNextUid();
};

View File

@ -106,6 +106,49 @@ SectionWidget* ContainerWidget::addSectionContent(const SectionContent::RefPtr&
return dropContent(data, sw, area, false);
}
bool ContainerWidget::removeSectionContent(const SectionContent::RefPtr& sc)
{
// Hide the content.
// The hideSectionContent() automatically deletes no longer required SectionWidget objects.
if (!hideSectionContent(sc))
return false;
// Begin of ugly work arround.
// TODO The hideSectionContent() method should take care of deleting FloatingWidgets and SectionWidgets,
// but only cares about SectionWidgets right now. So we need to check whether it was a FloatingWidget
// and delete it.
bool found = false;
for (int i = 0; i < _floatings.count(); ++i)
{
FloatingWidget* fw = _floatings.at(i);
InternalContentData data;
if (!(found = fw->takeContent(data)))
continue;
_floatings.removeAll(fw);
delete fw;
delete data.titleWidget;
delete data.contentWidget;
break;
} // End of ugly work arround.
// Get from hidden contents and delete associated internal stuff.
if (!_hiddenSectionContents.contains(sc->uid()))
{
qFatal("Something went wrong... The content should have been there :-/");
return false;
}
// Delete internal objects.
HiddenSectionItem hsi = _hiddenSectionContents.take(sc->uid());
delete hsi.data.titleWidget;
delete hsi.data.contentWidget;
// Hide the custom widgets of SectionContent.
// ... should we? ...
return true;
}
bool ContainerWidget::showSectionContent(const SectionContent::RefPtr& sc)
{
// Search SC in floatings
@ -603,6 +646,19 @@ QRect ContainerWidget::outerLeftDropRect() const
return QRect(r.left(), r.top(), w, r.height());
}
QList<SectionContent::RefPtr> ContainerWidget::contents() const
{
QList<SectionContent::WeakPtr> wl = _scLookupMapById.values();
QList<SectionContent::RefPtr> sl;
for (int i = 0; i < wl.count(); ++i)
{
const SectionContent::RefPtr sc = wl.at(i).toStrongRef();
if (sc)
sl.append(sc);
}
return sl;
}
///////////////////////////////////////////////////////////////////////
// PRIVATE API BEGINS HERE
///////////////////////////////////////////////////////////////////////

View File

@ -21,6 +21,7 @@ SectionContentWidget::SectionContentWidget(SectionContent::RefPtr c, QWidget* pa
SectionContentWidget::~SectionContentWidget()
{
qDebug() << Q_FUNC_INFO;
layout()->removeWidget(_content->contentWidget());
}
ADS_NAMESPACE_END

View File

@ -43,6 +43,7 @@ SectionTitleWidget::SectionTitleWidget(SectionContent::RefPtr content, QWidget*
SectionTitleWidget::~SectionTitleWidget()
{
qDebug() << Q_FUNC_INFO;
layout()->removeWidget(_content->titleWidget());
}
bool SectionTitleWidget::isActiveTab() const

View File

@ -22,14 +22,19 @@ windows {
SOURCES += \
src/main.cpp \
src/mainwindow.cpp \
src/icontitlewidget.cpp
src/icontitlewidget.cpp \
src/dialogs/SectionContentListModel.cpp \
src/dialogs/SectionContentListWidget.cpp
HEADERS += \
src/mainwindow.h \
src/icontitlewidget.h
src/icontitlewidget.h \
src/dialogs/SectionContentListModel.h \
src/dialogs/SectionContentListWidget.h
FORMS += \
src/mainwindow.ui
src/mainwindow.ui \
src/dialogs/SectionContentListWidget.ui
# Dependency: AdvancedDockingSystem (staticlib)

View File

@ -0,0 +1,85 @@
#include "SectionContentListModel.h"
SectionContentListModel::SectionContentListModel(QObject* parent) :
QAbstractTableModel(parent)
{
_headers.insert(UidColumn, "UID");
_headers.insert(UniqueNameColumn, "Unique Name");
_headers.insert(TitleColumn, "Title");
}
SectionContentListModel::~SectionContentListModel()
{
}
void SectionContentListModel::init(ADS_NS::ContainerWidget* cw)
{
beginResetModel();
_cw = cw;
_contents = _cw->contents();
endResetModel();
}
int SectionContentListModel::columnCount(const QModelIndex& parent) const
{
return _headers.count();
}
QVariant SectionContentListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return _headers.value(section);
return QVariant();
}
int SectionContentListModel::rowCount(const QModelIndex& parent) const
{
return _contents.count();
}
QVariant SectionContentListModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || index.row() > rowCount(index) - 1)
return QVariant();
const ADS_NS::SectionContent::RefPtr sc = _contents.at(index.row());
if (sc.isNull())
return QVariant();
switch (role)
{
case Qt::DisplayRole:
{
switch (index.column())
{
case UidColumn:
return sc->uid();
case UniqueNameColumn:
return sc->uniqueName();
case TitleColumn:
return sc->title();
}
}
}
return QVariant();
}
bool SectionContentListModel::removeRows(int row, int count, const QModelIndex& parent)
{
if (row > rowCount(parent) - 1)
return false;
const int first = row;
const int last = row + count - 1;
beginRemoveRows(parent, first, last);
for (int i = last; i >= first; --i)
{
const ADS_NS::SectionContent::RefPtr sc = _contents.at(i);
_cw->removeSectionContent(sc);
_contents.removeAt(i);
}
endRemoveRows();
return true;
}

View File

@ -0,0 +1,47 @@
#ifndef ADS_SECTIONCONTENTMODEL_H
#define ADS_SECTIONCONTENTMODEL_H
#include <QHash>
#include <QList>
#include <QString>
#include <QAbstractTableModel>
#include "ads/API.h"
#include "ads/ContainerWidget.h"
#include "ads/SectionContent.h"
ADS_NAMESPACE_BEGIN
class ContainerWidget;
ADS_NAMESPACE_END
class SectionContentListModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum Column
{
UidColumn,
UniqueNameColumn,
TitleColumn,
};
SectionContentListModel(QObject* parent);
virtual ~SectionContentListModel();
void init(ADS_NS::ContainerWidget* cw);
virtual int columnCount(const QModelIndex &parent) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
virtual int rowCount(const QModelIndex &parent) const;
virtual QVariant data(const QModelIndex &index, int role) const;
virtual bool removeRows(int row, int count, const QModelIndex &parent);
private:
QHash<int, QString> _headers;
ADS_NS::ContainerWidget* _cw;
QList<ADS_NS::SectionContent::RefPtr> _contents;
};
#endif

View File

@ -0,0 +1,38 @@
#include "SectionContentListWidget.h"
#include "SectionContentListModel.h"
SectionContentListWidget::SectionContentListWidget(QWidget* parent) :
QDialog(parent)
{
_ui.setupUi(this);
connect(_ui.deleteButton, SIGNAL(clicked(bool)), this, SLOT(onDeleteButtonClicked()));
}
void SectionContentListWidget::setValues(const SectionContentListWidget::Values& v)
{
_v = v;
// Reset
QAbstractItemModel* m = _ui.tableView->model();
if (m)
{
_ui.tableView->setModel(NULL);
delete m;
m = NULL;
}
// Fill.
SectionContentListModel* sclm = new SectionContentListModel(this);
sclm->init(_v.cw);
_ui.tableView->setModel(sclm);
}
void SectionContentListWidget::onDeleteButtonClicked()
{
const QModelIndex mi = _ui.tableView->currentIndex();
if (!mi.isValid())
return;
_ui.tableView->model()->removeRows(mi.row(), 1, mi.parent());
}

View File

@ -0,0 +1,33 @@
#ifndef SECTIONCONTENTLISTWIDGET
#define SECTIONCONTENTLISTWIDGET
#include <QDialog>
#include "ui_sectioncontentlistwidget.h"
#include "ads/API.h"
#include "ads/ContainerWidget.h"
#include "ads/SectionContent.h"
class SectionContentListWidget : public QDialog
{
Q_OBJECT
public:
class Values
{
public:
ADS_NS::ContainerWidget* cw;
};
SectionContentListWidget(QWidget* parent);
void setValues(const Values& v);
private slots:
void onDeleteButtonClicked();
private:
Ui::SectionContentListWidgetForm _ui;
Values _v;
};
#endif

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SectionContentListWidgetForm</class>
<widget class="QWidget" name="SectionContentListWidgetForm">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>522</width>
<height>258</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTableView" name="tableView">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="deleteButton">
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -12,6 +12,8 @@
#include "ads/SectionWidget.h"
#include "dialogs/SectionContentListWidget.h"
#include "icontitlewidget.h"
///////////////////////////////////////////////////////////////////////
@ -86,6 +88,9 @@ MainWindow::MainWindow(QWidget *parent) :
{
ui->setupUi(this);
// Setup actions.
QObject::connect(ui->actionContentList, SIGNAL(triggered()), this, SLOT(showSectionContentListDialog()));
// ADS - Create main container (ContainerWidget).
_container = new ADS_NS::ContainerWidget();
_container->setOrientation(Qt::Vertical);
@ -126,6 +131,16 @@ MainWindow::~MainWindow()
delete ui;
}
void MainWindow::showSectionContentListDialog()
{
SectionContentListWidget::Values v;
v.cw = _container;
SectionContentListWidget w(this);
w.setValues(v);
w.exec();
}
void MainWindow::onActiveTabChanged(const ADS_NS::SectionContent::RefPtr& sc, bool active)
{
Q_UNUSED(active);

View File

@ -18,6 +18,9 @@ public:
explicit MainWindow(QWidget *parent = 0);
virtual ~MainWindow();
public slots:
void showSectionContentListDialog();
private slots:
#if QT_VERSION >= 0x050000
void onActiveTabChanged(const ADS_NS::SectionContent::RefPtr& sc, bool active);

View File

@ -34,7 +34,7 @@
<property name="title">
<string>View</string>
</property>
<addaction name="actionDemo_1"/>
<addaction name="actionContentList"/>
<addaction name="actionDemo_2"/>
<addaction name="actionDemo_3"/>
</widget>
@ -52,9 +52,9 @@
<string>Add SectionContent</string>
</property>
</action>
<action name="actionDemo_1">
<action name="actionContentList">
<property name="text">
<string>Demo 1</string>
<string>Contents...</string>
</property>
</action>
<action name="actionDemo_2">