RedstartSessionList.h 5.52 KB
Newer Older
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
1
2
3
4
5
6
/*
 *  --------------------------------------------------------------------------------------------
 *
 *    VVV        VVV A           Virtual Acoustics (VA) | http://www.virtualacoustics.org
 *     VVV      VVV AAA          Licensed under the Apache License, Version 2.0
 *      VVV    VVV   AAA
7
 *       VVV  VVV     AAA        Copyright 2015-2020
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
8
9
10
11
12
13
14
15
16
17
 *        VVVVVV       AAA       Institute of Technical Acoustics (ITA)
 *         VVVV         AAA      RWTH Aachen University
 *
 *  --------------------------------------------------------------------------------------------
 */
#ifndef IW_REDSTART_SESSION_LIST
#define IW_REDSTART_SESSION_LIST

#include <QListView>
#include <QStringListModel>
18
#include <QSettings>
Dipl.-Ing. Jonas Stienen's avatar
Mooore    
Dipl.-Ing. Jonas Stienen committed
19
#include <qnamespace.h>
20
21
22
23
24

#include <VAException.h>
#include <VAStruct.h>

#include "RedstartUtils.h"
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

class RedstartSessionListModel : public QStringListModel
{};


class RedstartSessionListView : public QListView
{
	Q_OBJECT
public:
	inline RedstartSessionListView( QWidget* pParent )
		: QListView( pParent )
		, m_pModel( NULL )
	{
		m_pModel = new RedstartSessionListModel();
		this->setModel( m_pModel );

41
42
43
44
45
		// Initilizes session container in settings, if not present
		if( !m_qSettings.contains( "Redstart/Sessions" ) )
			m_qSettings.setValue( "Redstart/Sessions", QMap< QString, QVariant>() );

		UpdateSessionList();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
46

47
48
		QString sLastSessionName = m_qSettings.value( "Redstart/last_session" ).toString();
		SetCurrentSession( sLastSessionName );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
49
50
51
52
53

		setEditTriggers( QAbstractItemView::NoEditTriggers );
		setSelectionMode( QAbstractItemView::SingleSelection );
	};

54
55
	inline ~RedstartSessionListView()
	{
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
56
		QString sCurrentSessionName = m_pModel->data( currentIndex(), 0 ).toString();
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
		m_qSettings.setValue( "Redstart/last_session", sCurrentSessionName );
	};

	inline void SetCurrentSession( const QString& sID )
	{
		QStringList list;
		QMap< QString, QVariant > sessions = m_qSettings.value( "Redstart/Sessions" ).toMap();
		QMap< QString, QVariant>::const_iterator cit = sessions.constBegin();
		int i = -1;
		while( cit != sessions.constEnd() )
		{
			i++;
			if( cit++.key() == sID && i < m_pModel->rowCount() )
			{
				setCurrentIndex( m_pModel->index( i, 0 ) );
				return;
			}
		}

		if( m_pModel->rowCount() > 0 )
77
			this->setCurrentIndex( m_pModel->index( 0, 0 ) );
78
79
80
81
82
83
84
	};

	inline void UpdateSessionList()
	{
		QMap< QString, QVariant > sessions = m_qSettings.value( "Redstart/Sessions" ).toMap();

		QStringList list;
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
85
86
		if( sessions.count() > 0 )
		{
87

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
88
89
90
91
			QMap< QString, QVariant>::const_iterator cit = sessions.constBegin();
			while( cit != sessions.constEnd() )
				list.push_back( cit++.key() );
		}
92
93
94
		m_pModel->setStringList( list );
	};

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
95
	inline QString GetCurrentSessionID() const
96
97
98
99
100
	{
		QModelIndex index = currentIndex();
		if( m_pModel->rowCount() == 0 || !index.isValid() )
			return QString();

Dipl.-Ing. Jonas Stienen's avatar
Mooore    
Dipl.-Ing. Jonas Stienen committed
101
		QString sSessionName = m_pModel->data( index, 0 ).toString();
102
103
104
		return sSessionName;
	};

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
105
106
107
108
109
110
111
112
113
114
115
116
117
	inline void RemoveCurrentSession()
	{
		QMap< QString, QVariant > sessions = m_qSettings.value( "Redstart/Sessions" ).toMap();
		sessions.remove( GetCurrentSessionID() );
		m_qSettings.setValue( "Redstart/Sessions", sessions );
		m_qSettings.sync();

		UpdateSessionList();

		if( m_pModel->rowCount() > 0 )
			setCurrentIndex( m_pModel->index( 0, 0 ) );
	};

118
119
120
121
122
123
	inline bool SessionNameExists( const QString& sID )
	{
		QMap< QString, QVariant > sessions = m_qSettings.value( "Redstart/Sessions" ).toMap();
		return ( sessions.find( sID ) != sessions.end() );
	};

124
125
126
	inline void AddSession( const QString& sID, const QVariantHash& oCoreConfig, const bool bMakeCurrent = true )
	{
		QMap< QString, QVariant > sessions = m_qSettings.value( "Redstart/Sessions" ).toMap();
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
		sessions[ sID ] = oCoreConfig; // More secure than insert, will also overwrite
		m_qSettings.setValue( "Redstart/Sessions", sessions );

		UpdateSessionList();

		if( bMakeCurrent )
			SetCurrentSession( sID );
	};

	inline void RenameSession( const QString& sCurrentID, const QString& sNewID )
	{
		QMap< QString, QVariant > sessions = m_qSettings.value( "Redstart/Sessions" ).toMap();
		sessions[ sNewID ] = sessions[ sCurrentID ];
		sessions.remove( sCurrentID );
		m_qSettings.setValue( "Redstart/Sessions", sessions );

		UpdateSessionList();
	};

	inline void UpdateSession( const QString& sID, const QVariantHash& oCoreConfig, const bool bMakeCurrent = true )
	{
		QMap< QString, QVariant > sessions = m_qSettings.value( "Redstart/Sessions" ).toMap();

		if( sessions.find( sID ) == sessions.end() )
			return;

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
		sessions[ sID ] = oCoreConfig;
		m_qSettings.setValue( "Redstart/Sessions", sessions );

		UpdateSessionList();

		if( bMakeCurrent )
			SetCurrentSession( sID );
	};

	inline CVAStruct GetCurrentConfig()
	{
		if( m_pModel->rowCount() == 0 )
			VA_EXCEPT2( INVALID_PARAMETER, "No session vailable, please create one first." );

		QMap< QString, QVariant > sessions = m_qSettings.value( "Redstart/Sessions" ).toMap();
		QString sSessionName = GetCurrentSessionID();

		CVAStruct oCoreConfig;
		if( sessions.contains( sSessionName ) )
			oCoreConfig = ConvertQHashToVAStruct( sessions[ sSessionName ].toHash() );
		else
			VA_EXCEPT2( INVALID_PARAMETER, "Could not find Redstart session with identifier '" + sSessionName.toStdString() + "' in session list" );

		if( !oCoreConfig.HasKey( "Macros" ) )
			oCoreConfig[ "Macros" ] = CVAStruct();

		CVAStruct& oMacros( oCoreConfig[ "Macros" ] );
		if( !oMacros.HasKey( "ProjectName" ) )
			oMacros[ "ProjectName" ] = "Redstart";

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
183
		return oCoreConfig;
184
185
	};

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
186
187
188
	public slots :

private:
189
190
	RedstartSessionListModel* m_pModel;
	QSettings m_qSettings;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
191
192
193
194
};


#endif // IW_REDSTART_SESSION_LIST