/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /*************************************************************************** * mainwindow.cc * * Wed Sep 17 09:41:09 CEST 2008 * Copyright 2008 Bent Bisballe Nyeng * deva@aasimon.org ****************************************************************************/ /* * This file is part of Pracro. * * Pracro is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Pracro is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Pracro; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include "mainwindow.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "aboutbox.h" #include "messagebox.h" #include "macrodrawer.h" #include "macrowindow.h" #include "macro.h" #include "header.h" #include "template.h" #include "debug.h" class Dbg : public QLabel { public: Dbg() { setText("dbg"); } void mousePressEvent(QMouseEvent *) { dbg_toggle(); } }; MainWindow *gmainwindow = NULL; MainWindow::MainWindow(QString patientid, QString title, QString course, QString templ, QString host, quint16 port, QString user) : QMainWindow(0), // Qt::WindowContextHelpButtonHint netcom(host, port) { gmainwindow = this; closing = false; scrollarea = new QScrollArea(); setCentralWidget(scrollarea); central = new QWidget(); scrollarea->setWidget(central); scrollarea->setWidgetResizable(true); scrollarea->setFocusPolicy(Qt::NoFocus); central->setFocusPolicy(Qt::NoFocus); central->setLayout(new QVBoxLayout()); connect(&netcom, SIGNAL(gotReply(QDomDocument&)), this, SLOT(handle(QDomDocument&))); connect(&netcom, SIGNAL(networkError(QString, QString)), this, SLOT(showError(QString, QString))); isStored = false; header = NULL; this->patientid = patientid; this->user = user; this->course = course; this->templ = templ; setWindowTitle("Pracro - " + title); setWindowIcon(QIcon(":/icons/icon.png")); QStatusBar *status = statusBar(); aboutButton = new QPushButton("Pracro v."VERSION, status); aboutButton->setIcon(QIcon(":icons/icon.png")); aboutButton->setMaximumHeight(18); aboutButton->setFocusPolicy(Qt::NoFocus); connect(aboutButton, SIGNAL(clicked()), this, SLOT(showAbout())); status->addPermanentWidget(aboutButton); if(dbg_enabled()) { status->addPermanentWidget(new Dbg()); } QToolBar *toolbar = addToolBar("controls"); toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); QAction *close_commit = toolbar->addAction(tr("Close and commit")); close_commit->setShortcut(QKeySequence(tr("Ctrl+Alt+S"))); close_commit->setIcon(QPixmap(":icons/icon_close_commit.png")); QAction *close_no_commit = toolbar->addAction(tr("Close no commit")); close_no_commit->setIcon(QPixmap(":icons/icon_close_no_commit.png")); close_no_commit->setShortcut(QKeySequence(tr("Ctrl+Alt+X"))); /* QAction *close_discard = toolbar->addAction("Close discard"); close_discard->setIcon(QPixmap(":icons/icon_discard.png")); connect(close_discard, SIGNAL(triggered()), this, SLOT(closeDiscard())); */ toolbar->addSeparator(); connect(close_commit, SIGNAL(triggered()), this, SLOT(closeCommit())); connect(close_no_commit, SIGNAL(triggered()), this, SLOT(closeNoCommit())); setStatusBar(status); init(); } MainWindow::~MainWindow() { } void MainWindow::showAbout() { AboutBox aboutBox(this, Qt::Dialog); aboutBox.exec(); } bool MainWindow::hasOpen(void *me) { Entities::iterator i = entities.begin(); while(i != entities.end()) { if(me != i.value() && i.value()->isOpen()) return true; i++; } return false; } void MainWindow::enableEntities(bool enable) { Entities::iterator i = entities.begin(); while(i != entities.end()) { i.value()->widget()->setEnabled(enable); i++; } } void MainWindow::closeCommit() { if(closing) return; if(hasOpen(NULL)) { MessageBox::warning(this, tr("Close first."), tr("Close open macros first.")); return; } enableEntities(false); netcom.commit(); // isStored = true; closing = true; } void MainWindow::closeNoCommit() { if(closing) return; if(hasOpen(NULL)) { MessageBox::warning(this, tr("Close first."), tr("Close open macros first.")); return; } enableEntities(false); netcom.nocommit(); // isStored = true; closing = true; } void MainWindow::closeDiscard() { if(closing) return; if(hasOpen(NULL)) { MessageBox::warning(this, tr("Close first."), tr("Close open macros first.")); return; } MessageBox::StandardButton res = MessageBox::critical(this, tr("Discard"), tr("This session will NOT be " "stored in the journal.
" "Are you sure you want to continue?"), MessageBox::Yes | MessageBox::No); if(res == MessageBox::Yes) { enableEntities(false); netcom.discard(); // isStored = true; closing = true; } } void MainWindow::closeEvent(QCloseEvent *event) { if(closing) { emit isClosing(); return; } if(hasOpen(NULL)) { MessageBox::warning(this, tr("Close first."), tr("Close open macros first.")); event->ignore(); return; } if(netcom.sessionid == "" || isStored || MessageBox::critical(this, tr("Discard"), tr("This session will " "NOT be stored in " "the journal.
" "Are you sure you want to continue?"), MessageBox::Yes | MessageBox::No) == MessageBox::Yes) { if(netcom.sessionid != "" && !isStored) { netcom.discard(); // isStored = true; closing = true; event->ignore(); return; } QSettings settings("Aasimon.org", "Pracro"); settings.beginGroup("MainWindow"); settings.setValue("size", size()); settings.setValue("pos", pos()); settings.endGroup(); event->accept(); dbg_free(); } else { event->ignore(); } emit isClosing(); } void MainWindow::init() { QSettings settings("Aasimon.org", "Pracro"); settings.beginGroup("MainWindow"); resize(settings.value("size", QSize(700, 800)).toSize()); move(settings.value("pos", QPoint(0, 0)).toPoint()); settings.endGroup(); netcom.patientid = patientid; netcom.templ = templ; netcom.course = course; netcom.user = user; netcom.initConnection(); } void MainWindow::updateTemplateHeaders(QDomNode &node) { QDomElement elem = node.toElement(); if(elem.tagName() != "template") return; QString title = elem.attribute("title"); QString name = elem.attribute("name"); if(!header) { header = new QLabel(); QFont headerfont = header->font(); headerfont.setBold(true); headerfont.setPointSize(headerfont.pointSize() + 4); header->setFont(headerfont); header->setAlignment(Qt::AlignHCenter); central->layout()->addWidget(header); } header->setText(title); statusBar()->showMessage(title + " (" + name + ") - SessionID: " + netcom.sessionid); } void MainWindow::showError(QString title, QString text) { MessageBox::critical(this, title, text); } void MainWindow::updateMacros(QDomNodeList &nodes) { bool creation = entities.size() == 0; for(int j = 0; j < nodes.count(); j++) { QDomNode node = nodes.at(j); QDomElement elem = node.toElement(); if(elem.tagName() == "macro" || elem.tagName() == "header") { QString name = elem.attribute("name"); if(entities.find(j) == entities.end()) { Entity *e = NULL; if(elem.tagName() == "macro") { e = new Macro(entities, netcom, course, templ, scrollarea); } if(elem.tagName() == "header") { e = new Header(); } entities[j] = e; entities[j]->update(node); central->layout()->addWidget(e->widget()); } else { entities[j]->update(node); } continue; } showError("XML Error", "Expected macro/header tag. Got '" + elem.tagName() + "' tag."); continue; } if(creation) { for(int i = 0; i < entities.size(); i++) { Macro *macro = (Macro*)entities[i]; if(macro->isstatic == false) { macro->widget()->setFocus(); break; } } QVBoxLayout *l = (QVBoxLayout*)central->layout(); l->addStretch(1); } } void MainWindow::updateTemplates(QDomNodeList &nodes) { for(int j = 0; j < nodes.count(); j++) { QDomNode node = nodes.at(j); QDomElement elem = node.toElement(); if(elem.tagName() == "template") { QString name = elem.attribute("name"); if(entities.find(j) == entities.end()) { Template *e = new Template(netcom); connect(e, SIGNAL(setTemplate(QString)), this, SLOT(setTemplate(QString))); entities[j] = e; entities[j]->update(node); central->layout()->addWidget(e->widget()); if(j == 0) e->widget()->setFocus(); } else { entities[j]->update(node); } continue; } showError("XML Error", "Expected template tag. Got '" + elem.tagName() + "' tag."); continue; } } void MainWindow::setTemplate(QString name) { templ = name; Entities::iterator i = entities.begin(); while(i != entities.end()) { delete i.value()->widget(); // delete i.value(); i++; } entities.clear(); } void MainWindow::handle(QDomDocument &doc) { enableEntities(true); DEBUG(mainwindow, "Handle %s\n", doc.toString().toStdString().c_str()); QDomNodeList nodes = doc.documentElement().childNodes(); for(int j = 0; j < nodes.count(); j++) { QDomNode node = nodes.at(j); QDomElement element = node.toElement(); if(element.tagName() == "error") { showError("Pracro Server Error", element.text()); closing = false; continue; } if(element.tagName() != "template" && element.tagName() != "course") { showError("XML error", "Outer tag not a template or course."); continue; } if(element.tagName() == "template" && element.attribute("name") != templ) { showError("XML error", "Got template name that didn't match current session."); continue; } // Set headers. titles, etc... updateTemplateHeaders(node); if(element.tagName() == "course") { // Update macros, header and resumes. QDomNodeList templatenodes = node.childNodes(); updateTemplates(templatenodes); } if(element.tagName() == "template") { // Update macros, header and resumes. QDomNodeList macronodes = node.childNodes(); updateMacros(macronodes); } } if(closing) { isStored = true; close(); } }