Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Scientific Software
Takin
mag-core
Commits
38b890f4
Commit
38b890f4
authored
Jul 14, 2018
by
Tobias WEBER
Browse files
created session loading/saving menu system
parent
afd7983e
Changes
2
Hide whitespace changes
Inline
Side-by-side
tools/in20/mainwnd.cpp
View file @
38b890f4
...
...
@@ -5,10 +5,13 @@
* @license see 'LICENSE' file
*/
#define MAX_RECENT_FILES 16
#include
"mainwnd.h"
#include
"globals.h"
#include
"funcs.h"
#include
<QtWidgets/QFileDialog>
MainWnd
::
MainWnd
(
QSettings
*
pSettings
)
:
QMainWindow
(),
m_pSettings
(
pSettings
),
...
...
@@ -21,7 +24,7 @@ MainWnd::MainWnd(QSettings* pSettings)
g_pCLI
=
m_pCLI
;
this
->
setObjectName
(
"in20"
);
this
->
setWindowTitle
(
"IN20 Tool
"
);
SetCurrentFile
(
"
"
);
this
->
resize
(
800
,
600
);
...
...
@@ -36,10 +39,10 @@ MainWnd::MainWnd(QSettings* pSettings)
menuFile
->
addAction
(
acNew
);
menuFile
->
addSeparator
();
auto
*
acOpen
=
new
QAction
(
QIcon
::
fromTheme
(
"document-open"
),
"Open..."
,
m_pMenu
);
auto
*
menuOpenRecent
=
new
QMenu
(
"Open Recent"
,
m_pMenu
);
m_
menuOpenRecent
=
new
QMenu
(
"Open Recent"
,
m_pMenu
);
menuFile
->
addAction
(
acOpen
);
menuOpenRecent
->
setIcon
(
QIcon
::
fromTheme
(
"document-open-recent"
));
menuFile
->
addMenu
(
menuOpenRecent
);
m_
menuOpenRecent
->
setIcon
(
QIcon
::
fromTheme
(
"document-open-recent"
));
menuFile
->
addMenu
(
m_
menuOpenRecent
);
menuFile
->
addSeparator
();
auto
*
acSave
=
new
QAction
(
QIcon
::
fromTheme
(
"document-save"
),
"Save"
,
m_pMenu
);
auto
*
acSaveAs
=
new
QAction
(
QIcon
::
fromTheme
(
"document-save-as"
),
"Save As..."
,
m_pMenu
);
...
...
@@ -93,10 +96,10 @@ MainWnd::MainWnd(QSettings* pSettings)
connect
(
m_pBrowser
->
GetWidget
(),
&
FileBrowserWidget
::
TransferFiles
,
m_pWS
->
GetWidget
(),
&
WorkSpaceWidget
::
ReceiveFiles
);
connect
(
m_pWS
->
GetWidget
(),
&
WorkSpaceWidget
::
PlotDataset
,
m_pCurPlot
->
GetWidget
(),
&
Plotter
::
Plot
);
connect
(
m_pBrowser
->
GetWidget
(),
&
FileBrowserWidget
::
PlotDataset
,
m_pCurPlot
->
GetWidget
(),
&
Plotter
::
Plot
);
connect
(
acNew
,
&
QAction
::
triggered
,
this
,
&
MainWnd
::
New
Session
);
connect
(
acOpen
,
&
QAction
::
triggered
,
this
,
&
MainWnd
::
Open
Session
);
connect
(
acSave
,
&
QAction
::
triggered
,
this
,
&
MainWnd
::
Save
Session
);
connect
(
acSaveAs
,
&
QAction
::
triggered
,
this
,
&
MainWnd
::
Save
Session
As
);
connect
(
acNew
,
&
QAction
::
triggered
,
this
,
&
MainWnd
::
New
File
);
connect
(
acOpen
,
&
QAction
::
triggered
,
this
,
static_cast
<
void
(
MainWnd
::*
)()
>
(
&
MainWnd
::
Open
File
)
);
connect
(
acSave
,
&
QAction
::
triggered
,
this
,
static_cast
<
void
(
MainWnd
::*
)()
>
(
&
MainWnd
::
Save
File
)
);
connect
(
acSaveAs
,
&
QAction
::
triggered
,
this
,
&
MainWnd
::
Save
File
As
);
//connect(acPrefs, &QAction::triggered, this, ....); TODO
//connect(acAbout, &QAction::triggered, this, ....); TODO
connect
(
acAboutQt
,
&
QAction
::
triggered
,
this
,
[]()
{
qApp
->
aboutQt
();
});
...
...
@@ -118,6 +121,9 @@ MainWnd::MainWnd(QSettings* pSettings)
// restore settings
if
(
m_pSettings
)
{
if
(
m_pSettings
->
contains
(
"mainwnd/recentsessions"
))
SetRecentFiles
(
m_pSettings
->
value
(
"mainwnd/recentsessions"
).
toStringList
());
// restore window state
if
(
m_pSettings
->
contains
(
"mainwnd/geo"
))
this
->
restoreGeometry
(
m_pSettings
->
value
(
"mainwnd/geo"
).
toByteArray
());
...
...
@@ -159,6 +165,11 @@ void MainWnd::closeEvent(QCloseEvent *pEvt)
{
if
(
m_pSettings
)
{
// remove superfluous entries and save the recent files list
while
(
m_recentFiles
.
size
()
>
MAX_RECENT_FILES
)
m_recentFiles
.
pop_front
();
m_pSettings
->
setValue
(
"mainwnd/recentsessions"
,
m_recentFiles
);
// save window state
m_pSettings
->
setValue
(
"mainwnd/geo"
,
this
->
saveGeometry
());
m_pSettings
->
setValue
(
"mainwnd/state"
,
this
->
saveState
());
...
...
@@ -172,35 +183,147 @@ void MainWnd::closeEvent(QCloseEvent *pEvt)
/**
* File -> New
*/
void
MainWnd
::
New
Session
()
void
MainWnd
::
New
File
()
{
SetCurrentFile
(
""
);
}
/**
* File -> Open
*/
void
MainWnd
::
Open
Session
()
void
MainWnd
::
Open
File
()
{
QString
dirLast
=
m_pSettings
->
value
(
"mainwnd/sessiondir"
,
""
).
toString
();
QString
filename
=
QFileDialog
::
getOpenFileName
(
this
,
"Open File"
,
dirLast
,
"IN20 Files (*.in20 *.IN20)"
);
if
(
filename
==
""
||
!
QFile
::
exists
(
filename
))
return
;
if
(
OpenFile
(
filename
))
m_pSettings
->
setValue
(
"mainwnd/sessiondir"
,
QFileInfo
(
filename
).
path
());
}
/**
* File -> Save
*/
void
MainWnd
::
Save
Session
()
void
MainWnd
::
Save
File
()
{
if
(
m_curFile
==
""
)
SaveFileAs
();
else
SaveFile
(
m_curFile
);
}
/**
* File -> Save As
*/
void
MainWnd
::
SaveSessionAs
()
void
MainWnd
::
SaveFileAs
()
{
QString
dirLast
=
m_pSettings
->
value
(
"mainwnd/sessiondir"
,
""
).
toString
();
QString
filename
=
QFileDialog
::
getSaveFileName
(
this
,
"Save File"
,
dirLast
,
"IN20 Files (*.in20 *.IN20)"
);
if
(
filename
==
""
)
return
;
if
(
SaveFile
(
filename
))
m_pSettings
->
setValue
(
"mainwnd/sessiondir"
,
QFileInfo
(
filename
).
path
());
}
/**
* load File
*/
bool
MainWnd
::
OpenFile
(
const
QString
&
file
)
{
if
(
file
==
""
||
!
QFile
::
exists
(
file
))
return
false
;
// TODO
SetCurrentFile
(
file
);
AddRecentFile
(
file
);
return
true
;
}
/**
* save File
*/
bool
MainWnd
::
SaveFile
(
const
QString
&
file
)
{
if
(
file
==
""
)
return
false
;
// TODO
SetCurrentFile
(
file
);
AddRecentFile
(
file
);
return
true
;
}
/**
* add a file to the recent files menu
*/
void
MainWnd
::
AddRecentFile
(
const
QString
&
file
)
{
for
(
const
auto
&
recentfile
:
m_recentFiles
)
{
// file already in list?
if
(
recentfile
==
file
)
return
;
}
m_recentFiles
.
push_back
(
file
);
RebuildRecentFiles
();
}
/**
* set the recent file menu
*/
void
MainWnd
::
SetRecentFiles
(
const
QStringList
&
files
)
{
m_recentFiles
=
files
;
RebuildRecentFiles
();
}
/**
* create the "recent files" sub-menu
*/
void
MainWnd
::
RebuildRecentFiles
()
{
m_menuOpenRecent
->
clear
();
std
::
size_t
num_recent_files
=
0
;
for
(
auto
iter
=
m_recentFiles
.
rbegin
();
iter
!=
m_recentFiles
.
rend
();
++
iter
)
{
QString
filename
=
*
iter
;
auto
*
acFile
=
new
QAction
(
QIcon
::
fromTheme
(
"document"
),
filename
,
m_pMenu
);
connect
(
acFile
,
&
QAction
::
triggered
,
[
this
,
filename
]()
{
this
->
OpenFile
(
filename
);
});
m_menuOpenRecent
->
addAction
(
acFile
);
if
(
++
num_recent_files
>=
MAX_RECENT_FILES
)
break
;
}
}
/**
* remember current file and set window title
*/
void
MainWnd
::
SetCurrentFile
(
const
QString
&
file
)
{
static
const
QString
title
(
"IN20 Tool"
);
m_curFile
=
file
;
if
(
m_curFile
==
""
)
this
->
setWindowTitle
(
title
);
else
this
->
setWindowTitle
(
title
+
" -- "
+
m_curFile
);
}
tools/in20/mainwnd.h
View file @
38b890f4
...
...
@@ -12,6 +12,7 @@
#include
<QtWidgets/QMainWindow>
#include
<QtWidgets/QMdiArea>
#include
<QtWidgets/QMenuBar>
#include
<QtWidgets/QMenu>
#include
<QtWidgets/QStatusBar>
#include
"filebrowser.h"
...
...
@@ -34,18 +35,32 @@ private:
CommandLine
*
m_pCLI
=
nullptr
;
PlotterDock
*
m_pCurPlot
=
nullptr
;
QMenu
*
m_menuOpenRecent
=
nullptr
;
QStringList
m_recentFiles
;
QString
m_curFile
;
protected:
virtual
void
showEvent
(
QShowEvent
*
pEvt
)
override
;
virtual
void
closeEvent
(
QCloseEvent
*
pEvt
)
override
;
void
SetCurrentFile
(
const
QString
&
file
);
void
SetRecentFiles
(
const
QStringList
&
files
);
void
AddRecentFile
(
const
QString
&
file
);
void
RebuildRecentFiles
();
public:
MainWnd
(
QSettings
*
pSettings
=
nullptr
);
virtual
~
MainWnd
();
void
NewSession
();
void
OpenSession
();
void
SaveSession
();
void
SaveSessionAs
();
// session file menu operations
void
NewFile
();
void
OpenFile
();
void
SaveFile
();
void
SaveFileAs
();
// actual session file operations
bool
OpenFile
(
const
QString
&
file
);
bool
SaveFile
(
const
QString
&
file
);
};
#endif
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment