/apkstudio/src/editortabs.cpp

https://github.com/as0ler/Android-Tools · C++ · 310 lines · 289 code · 21 blank · 0 comment · 45 complexity · 5387d748262f8409e157a5f1850b77db MD5 · raw file

  1. #include <QDesktopServices>
  2. #include <QFileInfo>
  3. #include <QInputDialog>
  4. #include <QTabBar>
  5. #include <QTextCodec>
  6. #include <QTextStream>
  7. #include "include/coder.h"
  8. #include "include/coderhighlighter.h"
  9. #include "include/constants.h"
  10. #include "include/editortabs.h"
  11. #include "include/fileutils.h"
  12. #include "include/findreplace.h"
  13. #include "include/preferences.h"
  14. #include "include/qrc.h"
  15. #include "include/runner.h"
  16. #include "include/viewer.h"
  17. APP_NAMESPACE_START
  18. EditorTabs::EditorTabs(QWidget *parent)
  19. : QTabWidget(parent)
  20. {
  21. _connections << connect(parent, SIGNAL(editCopy()), this, SLOT(onEditCopy()));
  22. _connections << connect(parent, SIGNAL(editCut()), this, SLOT(onEditCut()));
  23. _connections << connect(parent, SIGNAL(editFind()), this, SLOT(onEditFind()));
  24. _connections << connect(parent, SIGNAL(editGoto()), this, SLOT(onEditGoto()));
  25. _connections << connect(parent, SIGNAL(editPaste()), this, SLOT(onEditPaste()));
  26. _connections << connect(parent, SIGNAL(editRedo()), this, SLOT(onEditRedo()));
  27. _connections << connect(parent, SIGNAL(editReplace()), this, SLOT(onEditReplace()));
  28. _connections << connect(parent, SIGNAL(editUndo()), this, SLOT(onEditUndo()));
  29. _connections << connect(parent, SIGNAL(fileClose()), this, SLOT(onFileClose()));
  30. _connections << connect(parent, SIGNAL(fileCloseAll()), this, SLOT(onFileCloseAll()));
  31. _connections << connect(parent, SIGNAL(fileOpen(QString)), this, SLOT(onFileOpen(QString)));
  32. _connections << connect(parent, SIGNAL(fileSave()), this, SLOT(onFileSave()));
  33. _connections << connect(parent, SIGNAL(fileSaveAll()), this, SLOT(onFileSaveAll()));
  34. _connections << connect(this, &QTabWidget::tabCloseRequested, this, &EditorTabs::onTabCloseRequested);
  35. _connections << connect(tabBar(), &QTabBar::tabMoved, this, &EditorTabs::onTabMoved);
  36. _connections << connect(this, &EditorTabs::currentChanged, this, &EditorTabs::onCurrentChanged);
  37. _connections << connect(this, SIGNAL(fileChanged(QString)), parent, SLOT(onFileChanged(QString)));
  38. _connections << connect(this, SIGNAL(fileSaved(QString)), parent, SLOT(onFileSaved(QString)));
  39. setMovable(true);
  40. setTabsClosable(true);
  41. }
  42. void EditorTabs::onCurrentChanged(const int i)
  43. {
  44. emit fileChanged(_files.key(i, QString()));
  45. if (i >= 0)
  46. {
  47. QWidget *w = widget(i);
  48. Coder *c;
  49. if (w && (c = dynamic_cast<Coder *>(w)) && _finder)
  50. {
  51. _finder->setEditor(c);
  52. }
  53. }
  54. }
  55. void EditorTabs::onEditCopy()
  56. {
  57. int i;
  58. if ((i = currentIndex()) >= 0)
  59. {
  60. QWidget *w = widget(i);
  61. Coder *c;
  62. if (w && (c = dynamic_cast<Coder *>(w)))
  63. {
  64. c->copy();
  65. }
  66. }
  67. }
  68. void EditorTabs::onEditCut()
  69. {
  70. int i;
  71. if ((i = currentIndex()) >= 0)
  72. {
  73. QWidget *w = widget(i);
  74. Coder *c;
  75. if (w && (c = dynamic_cast<Coder *>(w)))
  76. {
  77. c->cut();
  78. }
  79. }
  80. }
  81. void EditorTabs::onEditFind()
  82. {
  83. int i;
  84. if ((i = currentIndex()) >= 0)
  85. {
  86. QWidget *w = widget(i);
  87. Coder *c;
  88. if (w && (c = dynamic_cast<Coder *>(w)))
  89. {
  90. if (_finder)
  91. {
  92. _finder->close();
  93. }
  94. _finder = new FindReplace(false, w);
  95. _finder->setEditor(c);
  96. _finder->show();
  97. }
  98. }
  99. }
  100. void EditorTabs::onEditGoto()
  101. {
  102. int i;
  103. if ((i = currentIndex()) >= 0)
  104. {
  105. QWidget *w = widget(i);
  106. Coder *c;
  107. if (w && (c = dynamic_cast<Coder *>(w)))
  108. {
  109. bool ok;
  110. int r = QInputDialog::getInt(this, __("goto", "titles"), __("line_no", "forms"), 1, 1, c->document()->blockCount(), 1, &ok);
  111. if (ok)
  112. {
  113. c->setTextCursor(QTextCursor(c->document()->findBlockByLineNumber(r - 1)));
  114. }
  115. }
  116. }
  117. }
  118. void EditorTabs::onEditPaste()
  119. {
  120. int i;
  121. if ((i = currentIndex()) >= 0)
  122. {
  123. QWidget *w = widget(i);
  124. Coder *c;
  125. if (w && (c = dynamic_cast<Coder *>(w)) && c->canPaste())
  126. {
  127. c->paste();
  128. }
  129. }
  130. }
  131. void EditorTabs::onEditRedo()
  132. {
  133. int i;
  134. if ((i = currentIndex()) >= 0)
  135. {
  136. QWidget *w = widget(i);
  137. Coder *c;
  138. if (w && (c = dynamic_cast<Coder *>(w)))
  139. {
  140. c->redo();
  141. }
  142. }
  143. }
  144. void EditorTabs::onEditReplace()
  145. {
  146. int i;
  147. if ((i = currentIndex()) >= 0)
  148. {
  149. QWidget *w = widget(i);
  150. Coder *c;
  151. if (w && (c = dynamic_cast<Coder *>(w)))
  152. {
  153. if (_finder)
  154. {
  155. _finder->close();
  156. }
  157. _finder = new FindReplace(true, w);
  158. _finder->setEditor(c);
  159. _finder->show();
  160. }
  161. }
  162. }
  163. void EditorTabs::onEditUndo()
  164. {
  165. int i;
  166. if ((i = currentIndex()) >= 0)
  167. {
  168. QWidget *w = widget(i);
  169. Coder *c;
  170. if (w && (c = dynamic_cast<Coder *>(w)))
  171. {
  172. c->undo();
  173. }
  174. }
  175. }
  176. void EditorTabs::onFileClose()
  177. {
  178. onTabCloseRequested(currentIndex());
  179. }
  180. void EditorTabs::onFileCloseAll()
  181. {
  182. int i;
  183. while ((i = currentIndex()) >= 0)
  184. {
  185. onTabCloseRequested(i);
  186. }
  187. }
  188. void EditorTabs::onFileOpen(const QString &p)
  189. {
  190. int i;
  191. QWidget *w;
  192. if ((i = _files.value(p, -1)) >= 0)
  193. {
  194. w = widget(i);
  195. }
  196. else
  197. {
  198. QFileInfo fi(p);
  199. QString ext = fi.suffix();
  200. if (QString(EDITOR_EXT_CODER).contains(ext, Qt::CaseInsensitive))
  201. {
  202. Coder *c = new Coder(this);
  203. c->setPlainText(FileUtils::read(p));
  204. new CoderHighlighter(fi.suffix() + ".def", HIGHLIGHTER_THEME, c->document());
  205. w = c;
  206. }
  207. else if (QString(EDITOR_EXT_VIEWER).contains(ext, Qt::CaseInsensitive))
  208. {
  209. Viewer *v = new Viewer(this);
  210. v->setPixmap(QPixmap(p));
  211. v->zoomReset();
  212. w = v;
  213. }
  214. else
  215. {
  216. QDesktopServices::openUrl(QUrl(p));
  217. return;
  218. }
  219. _files.insert(p, i = addTab(w, _provider.icon(fi), fi.fileName()));
  220. }
  221. setCurrentIndex(i);
  222. setTabToolTip(i, p);
  223. w->setProperty(TAB_PROPERTY_PATH, p);
  224. w->setFocus();
  225. emit fileChanged(p);
  226. }
  227. void EditorTabs::onFileSave()
  228. {
  229. int i;
  230. if ((i = currentIndex()) >= 0)
  231. {
  232. onFileSave(i);
  233. }
  234. }
  235. void EditorTabs::onFileSave(const int i)
  236. {
  237. QWidget *w = widget(i);
  238. Coder *c;
  239. if (w && (c = dynamic_cast<Coder *>(w)))
  240. {
  241. QString p = w->property(TAB_PROPERTY_PATH).toString();
  242. QFileInfo fi(p);
  243. if (fi.exists() || fi.isFile())
  244. {
  245. QFile f(p);
  246. if (f.open(QIODevice::Text | QIODevice::Truncate | QIODevice::WriteOnly))
  247. {
  248. QTextStream s(&f);
  249. s.setCodec(QTextCodec::codecForMib(Preferences::get()->textEncoding()));
  250. s.setGenerateByteOrderMark(false);
  251. s << c->toPlainText();
  252. f.close();
  253. }
  254. emit fileSaved(p);
  255. }
  256. }
  257. }
  258. void EditorTabs::onFileSaveAll()
  259. {
  260. for (int i = 0; i < tabBar()->count(); i++)
  261. {
  262. onFileSave(i);
  263. }
  264. }
  265. void EditorTabs::onTabCloseRequested(const int i)
  266. {
  267. if (i >= 0)
  268. {
  269. QWidget *w = widget(i);
  270. QString p = w->property(TAB_PROPERTY_PATH).toString();
  271. _files.remove(p);
  272. removeTab(i);
  273. }
  274. }
  275. void EditorTabs::onTabMoved(const int from, const int to)
  276. {
  277. QString f = _files.key(from);
  278. QString t = _files.key(to);
  279. _files.insert(f, to);
  280. _files.insert(t, from);
  281. }
  282. EditorTabs::~EditorTabs()
  283. {
  284. Preferences::get()
  285. ->setSessionFiles(_files.keys())
  286. ->save();
  287. APP_CONNECTIONS_DISCONNECT
  288. }
  289. APP_NAMESPACE_END