138 lines
2.9 KiB
C++
138 lines
2.9 KiB
C++
|
|
#include "tabwidget.h"
|
||
|
|
#include "gsceditor.h"
|
||
|
|
#include "savedialog.h"
|
||
|
|
|
||
|
|
TabWidget::TabWidget(QWidget* aParent)
|
||
|
|
: QTabWidget(aParent)
|
||
|
|
{
|
||
|
|
setTabsClosable(true);
|
||
|
|
|
||
|
|
connect(this, &QTabWidget::tabCloseRequested, this, &TabWidget::closeTab);
|
||
|
|
}
|
||
|
|
|
||
|
|
QString TabWidget::tabPath(int aTabIndex) const
|
||
|
|
{
|
||
|
|
GSCEditor* editor = dynamic_cast<GSCEditor*>(widget(aTabIndex));
|
||
|
|
if (!editor) { return ""; }
|
||
|
|
|
||
|
|
QVariant pathProp = editor->property("Path");
|
||
|
|
if (!pathProp.isValid() || !pathProp.canConvert(QMetaType::fromType<QString>())) { return ""; }
|
||
|
|
|
||
|
|
return pathProp.toString();
|
||
|
|
}
|
||
|
|
|
||
|
|
int TabWidget::tabIndexFromPath(const QString &aPath) const
|
||
|
|
{
|
||
|
|
for (int i = 0; i < count(); i++)
|
||
|
|
{
|
||
|
|
const QString text = tabPath(i);
|
||
|
|
if (text == aPath)
|
||
|
|
{
|
||
|
|
return i;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
void TabWidget::refreshCurrentTab(bool aSaved)
|
||
|
|
{
|
||
|
|
QString text = tabText(currentIndex());
|
||
|
|
if (!aSaved && !text.contains("*"))
|
||
|
|
{
|
||
|
|
setTabText(currentIndex(), QString("%1*").arg(text));
|
||
|
|
}
|
||
|
|
if (aSaved && text.contains("*"))
|
||
|
|
{
|
||
|
|
setTabText(currentIndex(), text.replace("*", ""));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool TabWidget::closeTab(int aTabIndex)
|
||
|
|
{
|
||
|
|
if (aTabIndex == -1) { return false; }
|
||
|
|
|
||
|
|
QString text = tabText(aTabIndex);
|
||
|
|
if (!text.contains("*"))
|
||
|
|
{
|
||
|
|
emit tabClosed(tabPath(aTabIndex));
|
||
|
|
removeTab(aTabIndex);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
GSCEditor* editor = dynamic_cast<GSCEditor*>(widget(aTabIndex));
|
||
|
|
if (!editor) { return false; }
|
||
|
|
|
||
|
|
SaveDialog* saveDialog = new SaveDialog(this, text.replace("*", ""));
|
||
|
|
saveDialog->exec();
|
||
|
|
|
||
|
|
if (saveDialog->Saved())
|
||
|
|
{
|
||
|
|
editor->save();
|
||
|
|
emit tabClosed(tabPath(aTabIndex));
|
||
|
|
removeTab(aTabIndex);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
else if (!saveDialog->Cancelled())
|
||
|
|
{
|
||
|
|
emit tabClosed(tabPath(aTabIndex));
|
||
|
|
removeTab(aTabIndex);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
void TabWidget::closeAllTabs()
|
||
|
|
{
|
||
|
|
for (int i = 0; i < count(); i++)
|
||
|
|
{
|
||
|
|
closeTab(i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void TabWidget::closeCurrentTab()
|
||
|
|
{
|
||
|
|
closeTab(currentIndex());
|
||
|
|
}
|
||
|
|
|
||
|
|
void TabWidget::saveAllTabs()
|
||
|
|
{
|
||
|
|
for (int i = 0; i < count(); i++)
|
||
|
|
{
|
||
|
|
saveTab(i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void TabWidget::saveCurrentTab()
|
||
|
|
{
|
||
|
|
saveTab(currentIndex());
|
||
|
|
}
|
||
|
|
|
||
|
|
void TabWidget::saveTab(int aTabIndex)
|
||
|
|
{
|
||
|
|
GSCEditor* editor = dynamic_cast<GSCEditor*>(widget(aTabIndex));
|
||
|
|
if (!editor) { return; }
|
||
|
|
|
||
|
|
editor->save();
|
||
|
|
|
||
|
|
QString text = tabText(aTabIndex);
|
||
|
|
setTabText(aTabIndex, text.replace("*", ""));
|
||
|
|
}
|
||
|
|
|
||
|
|
void TabWidget::saveCurrentTabAs()
|
||
|
|
{
|
||
|
|
saveTabAs(currentIndex());
|
||
|
|
}
|
||
|
|
|
||
|
|
void TabWidget::saveTabAs(int aTabIndex)
|
||
|
|
{
|
||
|
|
GSCEditor* editor = dynamic_cast<GSCEditor*>(widget(aTabIndex));
|
||
|
|
if (!editor) { return; }
|
||
|
|
|
||
|
|
editor->saveAs();
|
||
|
|
|
||
|
|
QString text = tabText(aTabIndex);
|
||
|
|
setTabText(aTabIndex, text.replace("*", ""));
|
||
|
|
}
|