Guest User

Untitled

a guest
Oct 23rd, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. // .h
  2.  
  3. #ifndef MAINWINDOW_H
  4. #define MAINWINDOW_H
  5.  
  6. #include <QtGui>
  7. #include <QtCore>
  8.  
  9. class MainWindow : public QMainWindow
  10. {
  11.     Q_OBJECT
  12.  
  13. public:
  14.     explicit MainWindow(QWidget * parent = 0);
  15.  
  16. private slots:
  17.     void createTab();
  18.     void tabClose(int i);
  19.     void openFile();
  20.  
  21. private:
  22.     void initMenu();
  23.     void initWidget();
  24.  
  25.     QTabWidget * tab_;
  26.  
  27.     QMap<int,      QWidget*>   num_for_tab;
  28.     QMap<QWidget*, QTextEdit*> textedit_for_tab;
  29.     QMap<QWidget*, QString>    file_for_tab;
  30. };
  31.  
  32. #endif // MAINWINDOW_H
  33.  
  34.  
  35. // .cpp
  36.  
  37.  
  38.  
  39. #include "MainWindow.h"
  40.  
  41. MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent)
  42. {
  43.     initMenu();
  44.     initWidget();
  45. }
  46.  
  47. void MainWindow::initWidget()
  48. {
  49.     setWindowTitle("Theme Creator[*]");
  50.     resize(640, 480);
  51.  
  52.     tab_ = new QTabWidget(this);
  53.     tab_->setFocus();
  54.     tab_->setTabsClosable(true);
  55.     setCentralWidget(tab_);
  56.  
  57.     connect(tab_, SIGNAL(tabCloseRequested(int)), this, SLOT(tabClose(int)));
  58. }
  59.  
  60. void MainWindow::initMenu()
  61. {
  62.     QMenu * menuFile = menuBar()->addMenu("&Fichier");
  63.  
  64.     QAction * menuFileNew = menuFile->addAction("&Nouveau");
  65.     connect(menuFileNew, SIGNAL(triggered()),
  66.             this, SLOT(createTab()));
  67.  
  68.     QAction * menuFileOpen = menuFile->addAction("&Ouvrir");
  69.     connect(menuFileOpen, SIGNAL(triggered()),
  70.             this, SLOT(openFile()));
  71.  
  72.     QAction * menuFileSave = menuFile->addAction("Enregi&strer");
  73.  
  74.     QToolBar * toolBarFichier = addToolBar("Fichier");
  75.     toolBarFichier->setMovable(false);
  76.     toolBarFichier->addAction(menuFileNew);
  77.     toolBarFichier->addAction(menuFileOpen);
  78.     toolBarFichier->addAction(menuFileSave);
  79. }
  80.  
  81. void MainWindow::createTab()
  82. {
  83.     QWidget * page = new QWidget(this);
  84.  
  85.     QVBoxLayout * layout = new QVBoxLayout(page);
  86.     layout->setContentsMargins(0, 0, 0, 0);
  87.     page->setLayout(layout);
  88.  
  89.     QTextEdit * txtContenu = new QTextEdit(page);
  90.     layout->addWidget(txtContenu);
  91.  
  92.     textedit_for_tab.insert(page, txtContenu);
  93.     file_for_tab.insert(page, "");
  94.  
  95.     int compteur = 1;
  96.     while(num_for_tab.keys().contains(compteur))
  97.     {
  98.         ++compteur;
  99.     }
  100.     num_for_tab.insert(compteur, page);
  101.     txtContenu->setPlainText(QString::fromUtf8("n° : %1").arg(compteur));
  102.     tab_->addTab(page, QString("Document sans nom %1").arg(compteur));
  103.     //connect(txtContenu->document(), SIGNAL(modificationChanged(bool)),
  104.     //        this, SLOT(setWindowModified(bool)));
  105.     tab_->setCurrentWidget(page);
  106. }
  107.  
  108. void MainWindow::tabClose(int index)
  109. {
  110.     QWidget * w = tab_->widget(index);
  111.     if (w != NULL)
  112.     {
  113.         tab_->removeTab(index);
  114.         int compteur = num_for_tab.key(w);
  115.         num_for_tab.remove(compteur);
  116.         QTextEdit * editor = textedit_for_tab.value(w);
  117.         textedit_for_tab.remove(editor);
  118.         w->deleteLater();
  119.     }
  120. }
  121.  
  122. void MainWindow::openFile()
  123. {
  124.     QString filename = QFileDialog::getOpenFileName(this,tr("Open file"));
  125.     if (!filename.isEmpty())
  126.     {
  127.         QFile file(filename);
  128.         createTab();
  129.         QWidget * current_tab = tab_->currentWidget();
  130.         QTextEdit * txt_edit = textedit_for_tab.value(current_tab);
  131.         int index = tab_->currentIndex();
  132.         tab_->setTabText(index, filename.mid(filename.lastIndexOf("/") +1));
  133.         if (txt_edit != NULL)
  134.         {
  135.             if (file.exists())
  136.             {
  137.                 if (file.open(QIODevice::ReadOnly))
  138.                 {
  139.                     txt_edit->setText(file.readAll());
  140.                     file_for_tab.insert(current_tab, filename);
  141.                     file.close();
  142.                 }
  143.             }
  144.         }
  145.     }
  146.  
  147. }
Add Comment
Please, Sign In to add comment