From 28528963a8c112b54e23a83eb3d3247fff44fcad Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Tue, 7 Jun 2011 11:23:35 +0200 Subject: [PATCH] argumentparser is now a new class. node cannot be moved from scene (temp fix) --- argumentparser.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++ argumentparser.h | 34 +++++++++++++++ edge.cpp | 14 ++---- edge.h | 4 +- main.cpp | 59 ++++--------------------- mainwindow.ui | 2 +- node.cpp | 18 +++++++- node.h | 2 + qtmindmap.pro | 6 ++- qtmindmap.pro.user | 2 +- qtmindmap_hu.ts | 94 ++++++++++++++++++++++++++++++++++------ 11 files changed, 257 insertions(+), 84 deletions(-) create mode 100644 argumentparser.cpp create mode 100644 argumentparser.h diff --git a/argumentparser.cpp b/argumentparser.cpp new file mode 100644 index 0000000..b961009 --- /dev/null +++ b/argumentparser.cpp @@ -0,0 +1,106 @@ +#include "argumentparser.h" + +#include +#include +#include +#include + +#include + +ArgumentParser::ArgumentParser(QObject *parent) : + QObject(parent), + m_isSystemTray(false), + m_isShowMinimized(false), + m_filePath() +{ + qDebug() << __PRETTY_FUNCTION__; +} + + +void ArgumentParser::printUsage() +{ + qDebug() << __PRETTY_FUNCTION__; + + std::cout << tr("Usage: ").toStdString() << "qtmindmap [OPTION][FILE]" << std::endl + << tr("Mindmap program in QT").toStdString() << std::endl + << std::endl + << tr("Options:").toStdString() << std::endl + << "-h, --help\t\t" << tr("Prints this help.").toStdString() << std::endl + << "-t, --tray\t\t" << tr("Starts application in system tray.").toStdString() << std::endl + << "-s, --show-minimized\t" << tr("Hide main window, just show systray icon.").toStdString() << std::endl + << std::endl + << tr("Report bugs to: ").toStdString() << "denes.matetelki@gmail.com" << std::endl; +} + + +bool ArgumentParser::parseCmdLineArgs(bool &successful) +{ + qDebug() << __PRETTY_FUNCTION__; + + QStringList cmdlineArgs = QCoreApplication::arguments(); + cmdlineArgs.removeFirst(); + + QRegExp help("^-(h|-help)$"); + if (!cmdlineArgs.filter(help).isEmpty()) + { + printUsage(); + successful = true; + return false; + } + + QRegExp tray("^-(t|-tray)$"); + if (!cmdlineArgs.filter(tray).isEmpty()) m_isSystemTray = true; + + QRegExp minimized("^-(s|-show-minimized)$"); + if (!cmdlineArgs.filter(minimized).isEmpty()) m_isShowMinimized = true; + + /// @note It is an error? Shall it be handled? + // if (isSystemTray && isShowMinimized) return false; + + QRegExp all("^-(t|-tray|h|-help|s|-show-minimized)$"); + QStringList others; + foreach (QString arg, cmdlineArgs) if (all.indexIn(arg)==-1) others.append(arg); + if (others.size() > 1) + { + std::cerr << tr("Unkown options: ").toStdString() << others.join(" ").toStdString() << std::endl; + printUsage(); + successful = false; + return false; + } + + if (others.size()==1) + { + if (others.first().at(0)=='-') + { + successful = false; + return false; + } + + /// @note filecheck shall be done elsewhere? + m_filePath = others.first(); + QFileInfo fileInfo(m_filePath); + if (!fileInfo.exists()) + { + std::cerr << tr("File: ").toStdString() << m_filePath.toStdString() << tr(" does not exists.").toStdString() << std::endl; + successful = false; + return false; + } + if (!fileInfo.isFile()) + { + std::cerr << tr("File: ").toStdString() << m_filePath.toStdString() << tr(" is not a file.").toStdString() << std::endl; + successful = false; + return false; + } + if (!fileInfo.isReadable()) + { + std::cerr << tr("File: ").toStdString() << m_filePath.toStdString() << tr(" is not readable.").toStdString() << std::endl; + successful = false; + return false; + } + if (!fileInfo.isWritable()) + { + std::cout << tr("File: ").toStdString() << m_filePath.toStdString() << tr(" is not writeable.").toStdString() << std::endl; + } + } + return true; +} diff --git a/argumentparser.h b/argumentparser.h new file mode 100644 index 0000000..81daabe --- /dev/null +++ b/argumentparser.h @@ -0,0 +1,34 @@ +#ifndef ARGUMENTPARSER_H +#define ARGUMENTPARSER_H + +#include + +class ArgumentParser : public QObject +{ + Q_OBJECT + +public: + + explicit ArgumentParser(QObject *parent = 0); + + /*** + * @param successful is true is the program needs to stop but its not an error. + * @return true is program can continue + */ + bool parseCmdLineArgs(bool &successful); + + bool isSystemTray() { return m_isSystemTray; } + bool isShowMinimized() { return m_isShowMinimized; } + QString filePath() { return m_filePath; } + + +private: + + void printUsage(); + + bool m_isSystemTray; + bool m_isShowMinimized; + QString m_filePath; +}; + +#endif // ARGUMENTPARSER_H diff --git a/edge.cpp b/edge.cpp index 2ac4263..e602999 100644 --- a/edge.cpp +++ b/edge.cpp @@ -59,22 +59,14 @@ void Edge::adjust() if (!m_sourceNode || !m_destNode) return; - prepareGeometryChange(); QLineF line(mapFromItem(m_sourceNode, 0, 0) + m_sourceNode->boundingRect().center(), mapFromItem(m_destNode, 0, 0) + m_destNode->boundingRect().center()); - qreal length = line.length(); - - - - if (length > qreal(20.)) { - - QPointF sourceOffset(firstNotContainedPoint(line,m_sourceNode->sceneBoundingRect())); - QPointF destOffset(firstNotContainedPoint(line,m_destNode->sceneBoundingRect(),true)); - m_sourcePoint = sourceOffset; - m_destPoint = destOffset; + if (line.length() > qreal(20.)) { + m_sourcePoint = firstNotContainedPoint(line,m_sourceNode->sceneBoundingRect()); + m_destPoint = firstNotContainedPoint(line,m_destNode->sceneBoundingRect(),true); } else { m_sourcePoint = m_destPoint = line.p1(); } diff --git a/edge.h b/edge.h index 357ba6a..9f14268 100644 --- a/edge.h +++ b/edge.h @@ -15,8 +15,8 @@ public: void adjust(); - enum { Type = UserType + 2 }; - int type() const { return Type; } +// enum { Type = UserType + 2 }; +// int type() const { return Type; } protected: QRectF boundingRect() const; diff --git a/main.cpp b/main.cpp index 30ba2fb..935fd10 100644 --- a/main.cpp +++ b/main.cpp @@ -8,50 +8,10 @@ #include "mainwindow.h" #include "systemtray.h" +#include "argumentparser.h" -void printUsage() -{ - std::cout << "Usage: qtmindmap [OPTION][FILE]" << std::endl - << "Mindmap program in QT" << std::endl - << std::endl - << "Options:" << std::endl - << "-h, --help\tPrints this help." << std::endl - << "-t, --tray\tStarts application in system tray." << std::endl - << "-s, --show-minimized\tHide main window, just show systray icon." << std::endl - << std::endl - << "Report bugs to: denes.matetelki@gmail.com" << std::endl; -} - - -bool parseCmdLineArgs(bool &isSystemTray, - bool &isShowMinimized, - QString &filePath) -{ - QStringList cmdlineArgs = QCoreApplication::arguments(); - cmdlineArgs.removeFirst(); - - QRegExp help("^-(h|-help)$"); - if (!cmdlineArgs.filter(help).isEmpty()) return false; - QRegExp tray("^-(t|-tray)$"); - if (!cmdlineArgs.filter(tray).isEmpty()) isSystemTray = true; - - QRegExp minimized("^-(s|-show-minimized)$"); - if (!cmdlineArgs.filter(minimized).isEmpty()) isShowMinimized = true; - - /// @note It is an error? Shall it be handled? - // if (isSystemTray && isShowMinimized) return false; - - QRegExp all("^-(t|-tray|h|-help|s|-show-minimized)$"); - QStringList others; - foreach (QString arg, cmdlineArgs) if (all.indexIn(arg)==-1) others.append(arg); - if (others.size() > 1) return false; - - if (others.size()==1) filePath = others.first(); - - return true; -} int main(int argc, char *argv[]) @@ -65,7 +25,7 @@ int main(int argc, char *argv[]) QTranslator translator; if (!translator.load(QString("qtmindmap_") + locale)) { - std::cout << "No translation file for locale: " << locale.toStdString() << std::endl; + std::cerr << "No translation file for locale: " << locale.toStdString() << std::endl; } else { @@ -73,19 +33,17 @@ int main(int argc, char *argv[]) } // parse args - bool isSystemTray(false); - bool isShowMinimized(false); - QString filePath; - if (!parseCmdLineArgs(isSystemTray,isShowMinimized,filePath)) + ArgumentParser argParser; + bool success; + if (!argParser.parseCmdLineArgs(success)) { - printUsage(); - return EXIT_FAILURE; + return success ? EXIT_SUCCESS : EXIT_FAILURE; } // system tray? MainWindow w; SystemTray systemtray(&w); - if (isSystemTray or isShowMinimized) + if (argParser.isSystemTray() or argParser.isShowMinimized()) { if (!QSystemTrayIcon::isSystemTrayAvailable()) { @@ -95,11 +53,10 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } QApplication::setQuitOnLastWindowClosed(false); -// w.showSysTray(); systemtray.setup(); systemtray.show(); } - if (!isShowMinimized) w.show(); + if (!argParser.isShowMinimized()) w.show(); return a.exec(); } diff --git a/mainwindow.ui b/mainwindow.ui index f7ad762..4485505 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -107,7 +107,7 @@ Export - Ctrl+X + Ctrl+X diff --git a/node.cpp b/node.cpp index b596a2d..4a568d8 100644 --- a/node.cpp +++ b/node.cpp @@ -3,6 +3,7 @@ #include #include #include +#include Node::Node(GraphWidget *parent) : m_graph(parent) { @@ -35,8 +36,6 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) switch (change) { case ItemPositionHasChanged: -// if () - foreach (Edge *edge, m_edgeList) edge->adjust(); break; default: @@ -50,6 +49,8 @@ void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) { qDebug() << __PRETTY_FUNCTION__; + prevpt = event->pos(); + update(); QGraphicsTextItem::mousePressEvent(event); } @@ -58,10 +59,21 @@ void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { qDebug() << __PRETTY_FUNCTION__; + if (!scene()->sceneRect().contains(sceneBoundingRect())) + { + setPos(prevpt); + } + update(); QGraphicsTextItem::mouseReleaseEvent(event); } +void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + qDebug() << __PRETTY_FUNCTION__; + + QGraphicsTextItem::mouseMoveEvent(event); +} void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *w) { @@ -73,4 +85,6 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid painter->drawRect(QRect(boundingRect().topLeft().toPoint(), boundingRect().bottomRight().toPoint() - QPoint(1,1))); + + prevpt = pos(); } diff --git a/node.h b/node.h index 4bc23c4..1925842 100644 --- a/node.h +++ b/node.h @@ -20,12 +20,14 @@ protected: QVariant itemChange(GraphicsItemChange change, const QVariant &value); void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); private: QList m_edgeList; GraphWidget *m_graph; + QPointF prevpt; }; #endif // NODE_H diff --git a/qtmindmap.pro b/qtmindmap.pro index 74d3ace..131b63b 100644 --- a/qtmindmap.pro +++ b/qtmindmap.pro @@ -16,14 +16,16 @@ SOURCES += main.cpp\ graphwidget.cpp \ node.cpp \ edge.cpp \ - systemtray.cpp + systemtray.cpp \ + argumentparser.cpp HEADERS += mainwindow.h \ aboutdialog.h \ graphwidget.h \ node.h \ edge.h \ - systemtray.h + systemtray.h \ + argumentparser.h FORMS += mainwindow.ui \ aboutdialog.ui diff --git a/qtmindmap.pro.user b/qtmindmap.pro.user index 675c486..a2bd180 100644 --- a/qtmindmap.pro.user +++ b/qtmindmap.pro.user @@ -182,7 +182,7 @@ qtmindmap.pro false - false + true 3768 diff --git a/qtmindmap_hu.ts b/qtmindmap_hu.ts index 4fe339c..8c0c3d9 100644 --- a/qtmindmap_hu.ts +++ b/qtmindmap_hu.ts @@ -12,7 +12,7 @@ QtMindMap 0.1 - + QtMindMap 0.1 @@ -29,12 +29,83 @@ p, li { white-space: pre-wrap; } + + ArgumentParser + + + Usage: + Hasznalat: + + + + Mindmap program in QT + Agyterkep program QT-ben irva + + + + Options: + Opciok: + + + + Prints this help. + Kiirja ezt a szoveget. + + + + Starts application in system tray. + A programot blablabla-ban inditja. + + + + Hide main window, just show systray icon. + Nem mutatja a foablakot, csak a balbalba-t. + + + + Report bugs to: + Ide jelentsd a bugokat: + + + + Unkown options: + Ismeretlen opciok: + + + + + + + File: + Fajl: + + + + does not exists. + nem letezik. + + + + is not a file. + nem egy fajl. + + + + is not readable. + nem olvashato. + + + + is not writeable. + nem irhato. + + MainWindow QtMindMap - + QtMindMap @@ -79,43 +150,38 @@ p, li { white-space: pre-wrap; } E&xport - + E&xportalas Export - - - - - Ctrl+X - + Exportalas Export MindMap to image - + Az agyterkep kepkent exportalasa PNG image file (*.png) - + PNG kep file (*.png) MindMap exported as - + Az agyterkep exportalva lett, mint QObject - + QtMindMap Error QtMindMap hiba - + I couldn't detect any system tray on this system. Nem talalhato talca a jelenlegi rendszerben.