-
Notifications
You must be signed in to change notification settings - Fork 0
/
textedittabwidget.cpp
102 lines (81 loc) · 2.53 KB
/
textedittabwidget.cpp
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
94
95
96
97
98
99
100
101
102
#include <QtWidgets>
#include <QDebug>
#include <QMessageBox>
#include "textedittabwidget.h"
TextEditTabWidget::TextEditTabWidget(QWidget *parent) : QTabWidget(parent)
{
connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
}
TextEditTabWidget::~TextEditTabWidget()
{
}
void TextEditTabWidget::closeTab(int tabIndex)
{
if (tabIndex == -1)
return;
// Removes but, the page widget itself is not deleted.
QWidget* tab = this->widget(tabIndex);
this->removeTab(tabIndex);
numOfTabsOpen--;
// delete page
delete(tab);
tab = NULL;
}
void TextEditTabWidget::saveCurrentEditor()
{
int selectedIndex = this->currentIndex();
getEditorAtIndex(selectedIndex)->save();
}
CodeEditor * TextEditTabWidget::getEditorAtIndex(int i) const
{
QList<CodeEditor *> allEditors = this->findChildren<CodeEditor *>();
return allEditors[i];
}
QList<CodeEditor *> TextEditTabWidget::getAllEditors() const
{
return this->findChildren<CodeEditor *>();
}
void TextEditTabWidget::load(AssetManager *inAssetManager)
{
assetManager = inAssetManager;
}
void TextEditTabWidget::resizeEvent(QResizeEvent *e)
{
QTabWidget::resizeEvent(e);
}
bool TextEditTabWidget::addCodeTab(const QString &fileString)
{
// check if code tab exists
QList<CodeEditor *> allEditors = this->findChildren<CodeEditor *>();
for(int ie = 0; ie < allEditors.count(); ie++)
{
// if file already open then set it to focus
if(allEditors[ie]->getCurrentFileInfo()->absoluteFilePath() == fileString)
{
this->setCurrentIndex(this->indexOf(allEditors[ie]));
return false;
}
}
// create new code editor widget
CodeEditor * newCodeEditor = new CodeEditor();
newCodeEditor->load(assetManager);
// \/ causes asset manager variable to crash
if(!newCodeEditor->loadFile(fileString))
return false;
// move cursor back to top of page
QTextCursor cursor(newCodeEditor->textCursor());
cursor.movePosition(QTextCursor::Start);
newCodeEditor->setTextCursor(cursor);
newCodeEditor->setTabStopWidth(30);
newCodeEditor->setWordWrapMode(QTextOption::NoWrap);
QFont font = assetManager->getFont("MAIN_CODE_FONT");
font.setStyleHint(QFont::Monospace);
font.setFixedPitch(true);
newCodeEditor->setFont(font);
// add new widget
insertTab(0, newCodeEditor, newCodeEditor->getCurrentFileInfo()->fileName());
numOfTabsOpen++;
// set focus to newly created tab
this->setCurrentIndex(0);
return true;
}