Skip to content

Commit d2d2467

Browse files
committed
Adds possibility to remove section contents.
1 parent 9c958d0 commit d2d2467

15 files changed

+381
-6
lines changed

.astylerc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--style=allman
2+
3+
--indent=force-tab=4
4+
5+
--align-pointer=type
6+
--align-reference=type
7+
8+
--pad-oper
9+
--pad-header
10+
--unpad-paren
11+
12+
--remove-comment-prefix
13+
14+
--mode=c

AdvancedDockingSystem/include/ads/ContainerWidget.h

+13
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ class ADS_EXPORT_API ContainerWidget : public QFrame
5454
*/
5555
SectionWidget* addSectionContent(const SectionContent::RefPtr& sc, SectionWidget* sw = NULL, DropArea area = CenterDropArea);
5656

57+
/*!
58+
* Completely removes the <em>sc</em> from this ContainerWidget.
59+
* This container will no longer hold a reference to the content.
60+
* The content can be safely deleted.
61+
*/
62+
bool removeSectionContent(const SectionContent::RefPtr& sc);
63+
5764
/*!
5865
* Shows the specific SectionContent in UI.
5966
* Independed of the current state, whether it is used inside a section or is floating.
@@ -101,6 +108,12 @@ class ADS_EXPORT_API ContainerWidget : public QFrame
101108
QRect outerBottomDropRect() const;
102109
QRect outerLeftDropRect() const;
103110

111+
/*!
112+
* \brief contents
113+
* \return List of known SectionContent for this ContainerWidget.
114+
*/
115+
QList<SectionContent::RefPtr> contents() const;
116+
104117
private:
105118
//
106119
// Internal Stuff Begins Here

AdvancedDockingSystem/include/ads/SectionContent.h

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class ADS_EXPORT_API SectionContent
5757
// Optional attributes
5858
QString _title;
5959

60+
/* Note: This method could be a problem in static build environment
61+
* since it may begin with 0 for every module which uses ADS.
62+
*/
6063
static int GetNextUid();
6164
};
6265

AdvancedDockingSystem/src/ContainerWidget.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,49 @@ SectionWidget* ContainerWidget::addSectionContent(const SectionContent::RefPtr&
106106
return dropContent(data, sw, area, false);
107107
}
108108

109+
bool ContainerWidget::removeSectionContent(const SectionContent::RefPtr& sc)
110+
{
111+
// Hide the content.
112+
// The hideSectionContent() automatically deletes no longer required SectionWidget objects.
113+
if (!hideSectionContent(sc))
114+
return false;
115+
116+
// Begin of ugly work arround.
117+
// TODO The hideSectionContent() method should take care of deleting FloatingWidgets and SectionWidgets,
118+
// but only cares about SectionWidgets right now. So we need to check whether it was a FloatingWidget
119+
// and delete it.
120+
bool found = false;
121+
for (int i = 0; i < _floatings.count(); ++i)
122+
{
123+
FloatingWidget* fw = _floatings.at(i);
124+
InternalContentData data;
125+
if (!(found = fw->takeContent(data)))
126+
continue;
127+
_floatings.removeAll(fw);
128+
delete fw;
129+
delete data.titleWidget;
130+
delete data.contentWidget;
131+
break;
132+
} // End of ugly work arround.
133+
134+
// Get from hidden contents and delete associated internal stuff.
135+
if (!_hiddenSectionContents.contains(sc->uid()))
136+
{
137+
qFatal("Something went wrong... The content should have been there :-/");
138+
return false;
139+
}
140+
141+
// Delete internal objects.
142+
HiddenSectionItem hsi = _hiddenSectionContents.take(sc->uid());
143+
delete hsi.data.titleWidget;
144+
delete hsi.data.contentWidget;
145+
146+
// Hide the custom widgets of SectionContent.
147+
// ... should we? ...
148+
149+
return true;
150+
}
151+
109152
bool ContainerWidget::showSectionContent(const SectionContent::RefPtr& sc)
110153
{
111154
// Search SC in floatings
@@ -603,6 +646,19 @@ QRect ContainerWidget::outerLeftDropRect() const
603646
return QRect(r.left(), r.top(), w, r.height());
604647
}
605648

649+
QList<SectionContent::RefPtr> ContainerWidget::contents() const
650+
{
651+
QList<SectionContent::WeakPtr> wl = _scLookupMapById.values();
652+
QList<SectionContent::RefPtr> sl;
653+
for (int i = 0; i < wl.count(); ++i)
654+
{
655+
const SectionContent::RefPtr sc = wl.at(i).toStrongRef();
656+
if (sc)
657+
sl.append(sc);
658+
}
659+
return sl;
660+
}
661+
606662
///////////////////////////////////////////////////////////////////////
607663
// PRIVATE API BEGINS HERE
608664
///////////////////////////////////////////////////////////////////////

AdvancedDockingSystem/src/SectionContentWidget.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ SectionContentWidget::SectionContentWidget(SectionContent::RefPtr c, QWidget* pa
2121
SectionContentWidget::~SectionContentWidget()
2222
{
2323
qDebug() << Q_FUNC_INFO;
24+
layout()->removeWidget(_content->contentWidget());
2425
}
2526

2627
ADS_NAMESPACE_END

AdvancedDockingSystem/src/SectionTitleWidget.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ SectionTitleWidget::SectionTitleWidget(SectionContent::RefPtr content, QWidget*
4343
SectionTitleWidget::~SectionTitleWidget()
4444
{
4545
qDebug() << Q_FUNC_INFO;
46+
layout()->removeWidget(_content->titleWidget());
4647
}
4748

4849
bool SectionTitleWidget::isActiveTab() const

AdvancedDockingSystemDemo/AdvancedDockingSystemDemo.pro

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ windows {
2222
SOURCES += \
2323
src/main.cpp \
2424
src/mainwindow.cpp \
25-
src/icontitlewidget.cpp
25+
src/icontitlewidget.cpp \
26+
src/dialogs/SectionContentListModel.cpp \
27+
src/dialogs/SectionContentListWidget.cpp
2628

2729
HEADERS += \
2830
src/mainwindow.h \
29-
src/icontitlewidget.h
31+
src/icontitlewidget.h \
32+
src/dialogs/SectionContentListModel.h \
33+
src/dialogs/SectionContentListWidget.h
3034

3135
FORMS += \
32-
src/mainwindow.ui
36+
src/mainwindow.ui \
37+
src/dialogs/SectionContentListWidget.ui
3338

3439

3540
# Dependency: AdvancedDockingSystem (staticlib)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include "SectionContentListModel.h"
2+
3+
SectionContentListModel::SectionContentListModel(QObject* parent) :
4+
QAbstractTableModel(parent)
5+
{
6+
_headers.insert(UidColumn, "UID");
7+
_headers.insert(UniqueNameColumn, "Unique Name");
8+
_headers.insert(TitleColumn, "Title");
9+
}
10+
11+
SectionContentListModel::~SectionContentListModel()
12+
{
13+
}
14+
15+
void SectionContentListModel::init(ADS_NS::ContainerWidget* cw)
16+
{
17+
beginResetModel();
18+
_cw = cw;
19+
_contents = _cw->contents();
20+
endResetModel();
21+
}
22+
23+
int SectionContentListModel::columnCount(const QModelIndex& parent) const
24+
{
25+
return _headers.count();
26+
}
27+
28+
QVariant SectionContentListModel::headerData(int section, Qt::Orientation orientation, int role) const
29+
{
30+
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
31+
return _headers.value(section);
32+
return QVariant();
33+
}
34+
35+
int SectionContentListModel::rowCount(const QModelIndex& parent) const
36+
{
37+
return _contents.count();
38+
}
39+
40+
QVariant SectionContentListModel::data(const QModelIndex& index, int role) const
41+
{
42+
if (!index.isValid() || index.row() > rowCount(index) - 1)
43+
return QVariant();
44+
45+
const ADS_NS::SectionContent::RefPtr sc = _contents.at(index.row());
46+
if (sc.isNull())
47+
return QVariant();
48+
49+
switch (role)
50+
{
51+
case Qt::DisplayRole:
52+
{
53+
switch (index.column())
54+
{
55+
case UidColumn:
56+
return sc->uid();
57+
case UniqueNameColumn:
58+
return sc->uniqueName();
59+
case TitleColumn:
60+
return sc->title();
61+
}
62+
}
63+
}
64+
return QVariant();
65+
}
66+
67+
bool SectionContentListModel::removeRows(int row, int count, const QModelIndex& parent)
68+
{
69+
if (row > rowCount(parent) - 1)
70+
return false;
71+
72+
const int first = row;
73+
const int last = row + count - 1;
74+
beginRemoveRows(parent, first, last);
75+
76+
for (int i = last; i >= first; --i)
77+
{
78+
const ADS_NS::SectionContent::RefPtr sc = _contents.at(i);
79+
_cw->removeSectionContent(sc);
80+
_contents.removeAt(i);
81+
}
82+
83+
endRemoveRows();
84+
return true;
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef ADS_SECTIONCONTENTMODEL_H
2+
#define ADS_SECTIONCONTENTMODEL_H
3+
4+
#include <QHash>
5+
#include <QList>
6+
#include <QString>
7+
#include <QAbstractTableModel>
8+
9+
#include "ads/API.h"
10+
#include "ads/ContainerWidget.h"
11+
#include "ads/SectionContent.h"
12+
ADS_NAMESPACE_BEGIN
13+
class ContainerWidget;
14+
ADS_NAMESPACE_END
15+
16+
class SectionContentListModel : public QAbstractTableModel
17+
{
18+
Q_OBJECT
19+
20+
public:
21+
enum Column
22+
{
23+
UidColumn,
24+
UniqueNameColumn,
25+
TitleColumn,
26+
};
27+
28+
SectionContentListModel(QObject* parent);
29+
virtual ~SectionContentListModel();
30+
void init(ADS_NS::ContainerWidget* cw);
31+
32+
virtual int columnCount(const QModelIndex &parent) const;
33+
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
34+
35+
virtual int rowCount(const QModelIndex &parent) const;
36+
virtual QVariant data(const QModelIndex &index, int role) const;
37+
38+
virtual bool removeRows(int row, int count, const QModelIndex &parent);
39+
40+
private:
41+
QHash<int, QString> _headers;
42+
43+
ADS_NS::ContainerWidget* _cw;
44+
QList<ADS_NS::SectionContent::RefPtr> _contents;
45+
};
46+
47+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "SectionContentListWidget.h"
2+
#include "SectionContentListModel.h"
3+
4+
5+
SectionContentListWidget::SectionContentListWidget(QWidget* parent) :
6+
QDialog(parent)
7+
{
8+
_ui.setupUi(this);
9+
connect(_ui.deleteButton, SIGNAL(clicked(bool)), this, SLOT(onDeleteButtonClicked()));
10+
}
11+
12+
void SectionContentListWidget::setValues(const SectionContentListWidget::Values& v)
13+
{
14+
_v = v;
15+
16+
// Reset
17+
QAbstractItemModel* m = _ui.tableView->model();
18+
if (m)
19+
{
20+
_ui.tableView->setModel(NULL);
21+
delete m;
22+
m = NULL;
23+
}
24+
25+
// Fill.
26+
SectionContentListModel* sclm = new SectionContentListModel(this);
27+
sclm->init(_v.cw);
28+
_ui.tableView->setModel(sclm);
29+
}
30+
31+
void SectionContentListWidget::onDeleteButtonClicked()
32+
{
33+
const QModelIndex mi = _ui.tableView->currentIndex();
34+
if (!mi.isValid())
35+
return;
36+
37+
_ui.tableView->model()->removeRows(mi.row(), 1, mi.parent());
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef SECTIONCONTENTLISTWIDGET
2+
#define SECTIONCONTENTLISTWIDGET
3+
4+
#include <QDialog>
5+
#include "ui_sectioncontentlistwidget.h"
6+
7+
#include "ads/API.h"
8+
#include "ads/ContainerWidget.h"
9+
#include "ads/SectionContent.h"
10+
11+
class SectionContentListWidget : public QDialog
12+
{
13+
Q_OBJECT
14+
15+
public:
16+
class Values
17+
{
18+
public:
19+
ADS_NS::ContainerWidget* cw;
20+
};
21+
22+
SectionContentListWidget(QWidget* parent);
23+
void setValues(const Values& v);
24+
25+
private slots:
26+
void onDeleteButtonClicked();
27+
28+
private:
29+
Ui::SectionContentListWidgetForm _ui;
30+
Values _v;
31+
};
32+
33+
#endif

0 commit comments

Comments
 (0)