/
openfile.cpp.inl
93 lines (75 loc) · 3.77 KB
/
openfile.cpp.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifdef GUI_SUPPORT
QString CentralWidget::openFile(const QString &pFileName, File::Type pType,
const QString &pUrl, bool pShowWarning)
#else
QString openFile(const QString &pFileName, const File::Type &pType,
const QString &pUrl)
#endif
{
// Make sure that modes are available and that the file exists
#ifdef GUI_SUPPORT
if ((mModeTabs->count() == 0) || !QFile::exists(pFileName)) {
// Let the user know about us not being able to open the file, but only
// if we are not starting OpenCOR, i.e. only if our main window is
// visible
if (pShowWarning && mainWindow()->isVisible()) {
warningMessageBox(pUrl.isEmpty()?
QObject::tr("Open File"):
QObject::tr("Open Remote File"),
QObject::tr("<strong>%1</strong> could not be opened.").arg(pUrl.isEmpty()?
QDir::toNativeSeparators(pFileName):
pFileName));
}
return QObject::tr("'%1' could not be opened.").arg(pUrl.isEmpty()?
QDir::toNativeSeparators(pFileName):
pFileName);
}
#endif
// Check whether the file is already opened and, if so, select it and leave
QString fileName = canonicalFileName(pFileName);
#ifdef GUI_SUPPORT
for (int i = 0, iMax = mFileNames.count(); i < iMax; ++i) {
if (mFileNames[i] == fileName) {
setTabBarCurrentIndex(mFileTabs, i);
return {};
}
}
#endif
// Register the file with our file manager and get its status
FileManager::Status fileStatus = FileManager::instance()->manage(fileName, pType, pUrl);
if (fileStatus == FileManager::Status::DoesNotExist) {
return QObject::tr("'%1' could not be opened.").arg(pUrl.isEmpty()?
QDir::toNativeSeparators(pFileName):
pFileName);
}
// Create a new tab, insert it just after the current tab, set the full name
// of the file as the tool tip for the new tab, and make the new tab the
// current one
// Note #1: mFileNames is, for example, used to retrieve the name of a file,
// which we want to close (see closeFile()), so we must make sure
// that the order of its contents matches that of the tabs...
// Note #2: rather than using mFileNames, we could have used a tab's tool
// tip, but this makes it a bit tricky to handle with regards to
// connections and therefore with regards to some events triggering
// updateGui() to be called when the tool tip has not yet been
// assigned...
#ifdef GUI_SUPPORT
int fileTabIndex = mFileTabs->currentIndex()+1;
mFileNames.insert(fileTabIndex, fileName);
mFileTabs->insertTab(fileTabIndex, QString());
updateFileTab(fileTabIndex);
setTabBarCurrentIndex(mFileTabs, fileTabIndex);
#endif
// Everything went fine, so let our plugins know that our file has been
// opened
// Note: this requires using mLoadedFileHandlingPlugins, but it will not be
// set when we come here following OpenCOR's loading of settings,
// hence we do something similar to what is done in
// settingsLoaded()...
#ifdef GUI_SUPPORT
for (auto plugin : mLoadedFileHandlingPlugins) {
qobject_cast<FileHandlingInterface *>(plugin->instance())->fileOpened(fileName);
}
#endif
return {};
}