Skip to content

Commit 7a85c58

Browse files
committed
Refactor: Group main window initialization on opening project
For #168
1 parent 0e4dc9a commit 7a85c58

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/main/main_window.cpp

+23-23
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,7 @@ MainWindow::MainWindow() :
9696
// Open database
9797
QString lastOpen = Settings::lastOpenDatabaseFile.get();
9898
if (!lastOpen.isEmpty() && QFile(lastOpen).exists()) {
99-
setWindowTitleFilename(lastOpen);
100-
db.openExisting(this, lastOpen);
101-
updateFilters();
102-
setVisible(true);
103-
initCompositeBuffers();
104-
updateTableSize();
105-
setUIEnabled(true);
99+
attemptToOpenFile(lastOpen);
106100
}
107101

108102

@@ -449,6 +443,26 @@ void MainWindow::updateContextMenuEditIcon()
449443

450444
// PROJECT SETUP
451445

446+
/**
447+
* Attempts to open the given file and only changes UI if database initialization is successful.
448+
*
449+
* @param filepath The file to attempt to open.
450+
*/
451+
void MainWindow::attemptToOpenFile(const QString& filepath)
452+
{
453+
bool dbOpened = db.openExisting(this, filepath);
454+
455+
if (dbOpened) {
456+
setWindowTitleFilename(filepath);
457+
updateFilters();
458+
setVisible(true);
459+
initCompositeBuffers();
460+
updateTableSize();
461+
setUIEnabled(true);
462+
addToRecentFilesList(filepath);
463+
}
464+
}
465+
452466
/**
453467
* Prepares the composite tables and fills either all of them or only the one currently being
454468
* shown, with a status bar progress dialog.
@@ -953,14 +967,7 @@ void MainWindow::handle_openDatabase()
953967

954968
handle_closeDatabase();
955969

956-
setWindowTitleFilename(filepath);
957-
db.openExisting(this, filepath);
958-
updateFilters();
959-
initCompositeBuffers();
960-
updateTableSize();
961-
setUIEnabled(true);
962-
963-
addToRecentFilesList(filepath);
970+
attemptToOpenFile(filepath);
964971
}
965972

966973
/**
@@ -980,14 +987,7 @@ void MainWindow::handle_openRecentDatabase(QString filepath)
980987

981988
handle_closeDatabase();
982989

983-
setWindowTitleFilename(filepath);
984-
db.openExisting(this, filepath);
985-
updateFilters();
986-
initCompositeBuffers();
987-
updateTableSize();
988-
setUIEnabled(true);
989-
990-
addToRecentFilesList(filepath);
990+
attemptToOpenFile(filepath);
991991
}
992992

993993
/**

src/main/main_window.h

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class MainWindow : public QMainWindow, public Ui_MainWindow
9696
void updateContextMenuEditIcon();
9797

9898
// Project setup (on load)
99+
void attemptToOpenFile(const QString& filepath);
99100
void initCompositeBuffers();
100101

101102
// UI updates

0 commit comments

Comments
 (0)