summaryrefslogtreecommitdiffstats
path: root/utils/regtools/qeditor/mainwindow.cpp
blob: 1a88ebb47e471dc554e3345bf61a2090ef0c54f5 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <QWidget>
#include <QApplication>
#include <QDesktopWidget>
#include <QFileDialog>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QTabBar>
#include <QCloseEvent>
#include <QDebug>

#include "mainwindow.h"
#include "regtab.h"
#include "regedit.h"

MyTabWidget::MyTabWidget()
{
    setMovable(true);
    setTabsClosable(true);
    connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(OnCloseTab(int)));
}

bool MyTabWidget::CloseTab(int index)
{
    QWidget *w = this->widget(index);
    DocumentTab *doc = dynamic_cast< DocumentTab* >(w);
    if(doc->Quit())
    {
        removeTab(index);
        delete w;
        return true;
    }
    else
        return false;
}

void MyTabWidget::OnCloseTab(int index)
{
    CloseTab(index);
}

MainWindow::MainWindow(Backend *backend)
    :m_backend(backend)
{
    QAction *new_regtab_act = new QAction(QIcon::fromTheme("document-new"), tr("Register &Tab"), this);
    QAction *new_regedit_act = new QAction(QIcon::fromTheme("document-edit"), tr("Register &Editor"), this);
    QAction *load_desc_act = new QAction(QIcon::fromTheme("document-open"), tr("&Soc Description"), this);
    QAction *quit_act = new QAction(QIcon::fromTheme("application-exit"), tr("&Quit"), this);
    QAction *about_act = new QAction(QIcon::fromTheme("help-about"), tr("&About"), this);
    QAction *about_qt_act = new QAction(QIcon::fromTheme("help-about"), tr("About &Qt"), this);

    connect(new_regtab_act, SIGNAL(triggered()), this, SLOT(OnNewRegTab()));
    connect(new_regedit_act, SIGNAL(triggered()), this, SLOT(OnNewRegEdit()));
    connect(load_desc_act, SIGNAL(triggered()), this, SLOT(OnLoadDesc()));
    connect(quit_act, SIGNAL(triggered()), this, SLOT(OnQuit()));
    connect(about_act, SIGNAL(triggered()), this, SLOT(OnAbout()));
    connect(about_qt_act, SIGNAL(triggered()), this, SLOT(OnAboutQt()));

    QMenu *file_menu = menuBar()->addMenu(tr("&File"));
    QMenu *new_submenu = file_menu->addMenu(QIcon::fromTheme("document-new"), "&New");
    QMenu *load_submenu = file_menu->addMenu(QIcon::fromTheme("document-open"), "&Load");
    file_menu->addAction(quit_act);

    new_submenu->addAction(new_regtab_act);
    new_submenu->addAction(new_regedit_act);

    load_submenu->addAction(load_desc_act);

    QMenu *about_menu = menuBar()->addMenu(tr("&About"));
    about_menu->addAction(about_act);
    about_menu->addAction(about_qt_act);

    m_tab = new MyTabWidget();

    setCentralWidget(m_tab);

    ReadSettings();

    OnNewRegTab();
}

void MainWindow::ReadSettings()
{
    restoreGeometry(Settings::Get()->value("mainwindow/geometry").toByteArray());
}

void MainWindow::WriteSettings()
{
    Settings::Get()->setValue("mainwindow/geometry", saveGeometry());
}

void MainWindow::OnQuit()
{
    close();
}

void MainWindow::OnAbout()
{
    QMessageBox::about(this, "About", 
        "<h1>QEditor</h1>"
        "<h2>Version "APP_VERSION"</h2>"
        "<p>Written by Amaury Pouly</p>"
        "<p>Libraries:</p>"
        "<ul><li>soc_desc: "SOCDESC_VERSION"</li>"
#ifdef HAVE_HWSTUB
        "<li>hwstub: "HWSTUB_VERSION"</li>"
#else
        "<li>hwstub: not compiled in</li>"
#endif
        "</ul>");
}

void MainWindow::OnAboutQt()
{
    QMessageBox::aboutQt(this);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(!Quit())
        return event->ignore();
    WriteSettings();
    event->accept();
}

void MainWindow::OnLoadDesc()
{
    QFileDialog *fd = new QFileDialog(this);
    fd->setFilter("XML files (*.xml);;All files (*)");
    fd->setFileMode(QFileDialog::ExistingFiles);
    fd->setDirectory(Settings::Get()->value("loaddescdir", QDir::currentPath()).toString());
    if(fd->exec())
    {
        QStringList filenames = fd->selectedFiles();
        for(int i = 0; i < filenames.size(); i++)
            if(!m_backend->LoadSocDesc(filenames[i]))
            {
                QMessageBox msg;
                msg.setText(QString("Cannot load ") + filenames[i]);
                msg.exec();
            }
        Settings::Get()->setValue("loaddescdir", fd->directory().absolutePath());
    }
}

void MainWindow::OnTabModified(bool modified)
{
    QWidget *sender = qobject_cast< QWidget* >(QObject::sender());
    int index = m_tab->indexOf(sender);
    if(modified)
        m_tab->setTabIcon(index, QIcon::fromTheme("document-save"));
    else
        m_tab->setTabIcon(index, QIcon());
}

void MainWindow::AddTab(QWidget *tab, const QString& title)
{
    connect(tab, SIGNAL(OnModified(bool)), this, SLOT(OnTabModified(bool)));
    m_tab->setCurrentIndex(m_tab->addTab(tab, title));
}

void MainWindow::OnNewRegTab()
{
    AddTab(new RegTab(m_backend, this), "Register Tab");
}

void MainWindow::OnNewRegEdit()
{
    AddTab(new RegEdit(m_backend, this), "Register Editor");
}

bool MainWindow::Quit()
{
    while(m_tab->count() > 0)
    {
        if(!m_tab->CloseTab(0))
            return false;
    }
    return true;
}