Code: Alles auswählen
#include <QtGui>
#include "mainwindow.h"
#include "mdichild.h"
#include "adddialog.h"
#include "table.h"
#include <mysubwindow.h>
MainWindow::MainWindow()
{
mdiArea = new QMdiArea;
mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setCentralWidget(mdiArea);
connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(updateMenus()));
windowMapper = new QSignalMapper(this);
connect(windowMapper, SIGNAL(mapped(QWidget*)), this, SLOT(setActiveSubWindow(QWidget*)));
createActions();
createMenus();
createToolBars();
createStatusBar();
updateMenus();
addToolBar(Qt::LeftToolBarArea,fileToolBar);
setWindowTitle(tr("MDI"));
setUnifiedTitleAndToolBarOnMac(true);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
mdiArea->closeAllSubWindows();
if (mdiArea->currentSubWindow()) {
event->ignore();
} else {
event->accept();
}
}
void MainWindow::newFile()
{
sw = new QMdiSubWindow();
sw->setWidget(new AddDialog(sw));
sw->setAttribute(Qt::WA_DeleteOnClose);
mdiArea->addSubWindow(sw);
sw->show();
}
void MainWindow::open()
{
MySubWindow *sw = new MySubWindow();
table *t = new table(sw);
sw->setWidget(t);
sw->setAttribute(Qt::WA_DeleteOnClose);
mdiArea->addSubWindow(sw);
sw->resize(700,480);
sw->show();
connect(sw,SIGNAL(resizeTable(table *,int)),t,SLOT(getZ(table*,int)));
}
void MainWindow::save()
{
if (activeMdiChild())
activeMdiChild()->save();
}
void MainWindow::updateMenus()
{
bool hasMdiChild = (activeMdiChild());
if(hasMdiChild == 0)
hasMdiChild = (activeTMdiChild());
saveAct->setEnabled(hasMdiChild);
pasteAct->setEnabled(hasMdiChild);
closeAct->setEnabled(hasMdiChild);
closeAllAct->setEnabled(hasMdiChild);
tileAct->setEnabled(hasMdiChild);
cascadeAct->setEnabled(hasMdiChild);
nextAct->setEnabled(hasMdiChild);
previousAct->setEnabled(hasMdiChild);
separatorAct->setVisible(hasMdiChild);
}
void MainWindow::updateWindowMenu()
{
windowMenu->clear();
windowMenu->addAction(closeAct);
windowMenu->addAction(closeAllAct);
windowMenu->addSeparator();
windowMenu->addAction(tileAct);
windowMenu->addAction(cascadeAct);
windowMenu->addSeparator();
windowMenu->addAction(nextAct);
windowMenu->addAction(previousAct);
windowMenu->addAction(separatorAct);
QList<QMdiSubWindow *> windows = mdiArea->subWindowList();
separatorAct->setVisible(!windows.isEmpty());
for (int i = 0; i < windows.size(); ++i) {
AddDialog *child = qobject_cast<AddDialog *>(windows.at(i)->widget());
QString text;
if (i < 9) {
text = tr("Add %2").arg(i + 1);
} else {
text = tr("Add %2").arg(i + 1);
}
QAction *action = windowMenu->addAction(text);
action->setCheckable(true);
action ->setChecked(child == activeMdiChild());
connect(action, SIGNAL(triggered()), windowMapper, SLOT(map()));
windowMapper->setMapping(action, windows.at(i));
}
}
void MainWindow::createActions()
{
newAct = new QAction(QIcon(":/images/new.png"), tr("&Neu"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Neuer Eintrag"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
openAct = new QAction(QIcon(":/images/tabelle.png"), tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Tabelle Öffnen"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
saveAct = new QAction(QIcon(":/images/save.png"), tr("&Speichern"), this);
saveAct->setShortcuts(QKeySequence::Save);
saveAct->setStatusTip(tr("Speichern"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
sldAct = new QAction(QIcon(":/images/sld.png"),tr("Layout Ausrichtung wechseln"),this);
sldAct->setStatusTip("Layout Ausrichtung wechseln");
connect(sldAct, SIGNAL(triggered()), this, SLOT(switchLayoutDirection()));
exitAct = new QAction(QIcon(":/images/exit.png"), tr("Schließen"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Schließen"));
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
cutAct->setShortcuts(QKeySequence::Cut);
cutAct->setStatusTip(tr("Ausschneiden"));
connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
copyAct->setShortcuts(QKeySequence::Copy);
copyAct->setStatusTip(tr("Kopieren"));
connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
pasteAct->setShortcuts(QKeySequence::Paste);
pasteAct->setStatusTip(tr("Einfügen"));
connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
closeAct = new QAction(QIcon(":/images/exit.png"),tr("Schließen"), this);
closeAct->setStatusTip(tr("Schließt das aktive Fenster"));
connect(closeAct, SIGNAL(triggered()), mdiArea, SLOT(closeActiveSubWindow()));
closeAllAct = new QAction(QIcon(":/images/exit-all.png"),tr("Alle Schließen"), this);
closeAllAct->setStatusTip(tr("Alle Fenster schließen"));
connect(closeAllAct, SIGNAL(triggered()), mdiArea, SLOT(closeAllSubWindows()));
tileAct = new QAction(QIcon(":/images/tile.png"),tr("&Kacheln"), this);
tileAct->setStatusTip(tr("Fenster Kacheln"));
connect(tileAct, SIGNAL(triggered()), mdiArea, SLOT(tileSubWindows()));
cascadeAct = new QAction(QIcon(":/images/cascade.png"),tr("&Wassserfall"), this);
cascadeAct->setStatusTip(tr("Ordnet die Fenster wie einen Wasserfall an"));
connect(cascadeAct, SIGNAL(triggered()), mdiArea, SLOT(cascadeSubWindows()));
nextAct = new QAction(QIcon(":/images/arrow-rechts.png"),tr("&Nächstes"), this);
nextAct->setShortcuts(QKeySequence::NextChild);
nextAct->setStatusTip(tr("Wechselt zum nächstem Fenster"));
connect(nextAct, SIGNAL(triggered()), mdiArea, SLOT(activateNextSubWindow()));
previousAct = new QAction(QIcon(":/images/arrow-links.png"),tr("&Vorheriges"), this);
previousAct->setShortcuts(QKeySequence::PreviousChild);
previousAct->setStatusTip(tr("Wechselt zum vorherigem Fenster"));
connect(previousAct, SIGNAL(triggered()), mdiArea, SLOT(activatePreviousSubWindow()));
separatorAct = new QAction(this);
separatorAct->setSeparator(true);
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addSeparator();
fileMenu->addAction(sldAct);
fileMenu->addAction(exitAct);
windowMenu = menuBar()->addMenu(tr("&Window"));
updateWindowMenu();
connect(windowMenu, SIGNAL(aboutToShow()), this, SLOT(updateWindowMenu()));
}
void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
fileToolBar->addAction(openAct);
fileToolBar->addAction(saveAct);
}
void MainWindow::createStatusBar()
{
statusBar()->showMessage(tr("Fertig"));
}
AddDialog *MainWindow::activeMdiChild()
{
if (QMdiSubWindow *activeSubWindow = mdiArea->activeSubWindow())
return qobject_cast<AddDialog *>(activeSubWindow->widget());
return 0;
}
table *MainWindow::activeTMdiChild(){
if(QMdiSubWindow *activeSubWindow = mdiArea->activeSubWindow())
return qobject_cast<table *>(activeSubWindow->widget());
return 0;
}
void MainWindow::switchLayoutDirection()
{
if (layoutDirection() == Qt::LeftToRight)
qApp->setLayoutDirection(Qt::RightToLeft);
else
qApp->setLayoutDirection(Qt::LeftToRight);
}
void MainWindow::setActiveSubWindow(QWidget *window)
{
if (!window)
return;
mdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow *>(window));
}
void MainWindow::closee(){
mdiArea->closeActiveSubWindow();
}