Created
May 31, 2012 14:03
-
-
Save jelmervdl/2843603 to your computer and use it in GitHub Desktop.
Dact issue 56
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 2.6) | |
project(DACT) | |
set(CMAKE_CXX_FLAGS "-pthread") | |
LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir) | |
IF("${isSystemDir}" STREQUAL "-1") | |
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") | |
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) | |
ENDIF("${isSystemDir}" STREQUAL "-1") | |
if(APPLE) | |
set(CMAKE_OSX_ARCHITECTURES "x86_64") | |
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.5") | |
endif(APPLE) | |
set(ENABLE_BUNDLE OFF) | |
set(PROGNAME dact) | |
set(VERSION "0.0.1") | |
if (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) | |
set (CMAKE_BUILD_TYPE Release) | |
endif (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) | |
list(APPEND CMAKE_MODULE_PATH "${DACT_SOURCE_DIR}/cmake") | |
include_directories(${DACT_SOURCE_DIR}/include) | |
include_directories(${DACT_BINARY_DIR}) | |
find_package(Qt4 4.8.0 COMPONENTS QtCore QtGui QtMain QtNetwork REQUIRED) | |
INCLUDE(${QT_USE_FILE}) | |
# Dact sources | |
SET(dact_SRCS | |
src/DactApplication.cpp | |
src/DactTreeScene.cpp | |
src/DactTreeView.cpp | |
src/MainWindow.cpp | |
src/PopupItem.cpp | |
src/TreeNode.cpp | |
src/main.cpp | |
) | |
SET(dact_HDRS | |
include/DactApplication.hh | |
include/DactTreeScene.hh | |
include/DactTreeView.hh | |
include/MainWindow.hh | |
include/PopupItem.hh | |
include/TreeNode.hh | |
) | |
SET(dact_MOC_HDRS | |
include/DactApplication.hh | |
include/DactTreeScene.hh | |
include/DactTreeView.hh | |
include/MainWindow.hh | |
) | |
SET(dact_UI | |
ui/MainWindow.ui | |
) | |
set_source_files_properties( | |
${dact_MACOSX_RESOURCE_FILES} | |
PROPERTIES | |
MACOSX_PACKAGE_LOCATION Resources | |
) | |
qt4_add_resources(dact_SRCS ${dact_RCS}) | |
qt4_wrap_ui(dact_UI_HDRS ${dact_UI}) | |
qt4_wrap_cpp(dact_MOC_SRCS ${dact_MOC_HDRS}) | |
add_executable(${PROGNAME} WIN32 | |
${dact_SRCS} | |
${dact_HDRS} | |
${dact_MOC_SRCS} | |
${dact_UI_HDRS} | |
${dact_MACOSX_RESOURCE_FILES}) | |
target_link_libraries(${PROGNAME} ${QT_LIBRARIES}) | |
install(TARGETS ${PROGNAME} | |
BUNDLE DESTINATION . COMPONENT Runtime | |
RUNTIME DESTINATION bin COMPONENT Runtime) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DactApplication.hh" | |
DactApplication::DactApplication(int &argc, char** argv) | |
: | |
QApplication(argc, argv), | |
d_mainWindow(0) | |
{ | |
// | |
} | |
void DactApplication::init() | |
{ | |
d_mainWindow.reset(new MainWindow()); | |
d_mainWindow->show(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DACTAPPLICATION_H | |
#define DACTAPPLICATION_H | |
#include <QApplication> | |
#include <QScopedPointer> | |
#include "MainWindow.hh" | |
class DactApplication: public QApplication | |
{ | |
Q_OBJECT | |
public: | |
DactApplication(int &argc, char** argv); | |
void init(); | |
private: | |
QScopedPointer<MainWindow> d_mainWindow; | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DactTreeScene.hh" | |
#include "TreeNode.hh" | |
#include "PopupItem.hh" | |
DactTreeScene::DactTreeScene(QObject *parent) : | |
QGraphicsScene(parent) | |
{ | |
TreeNode *d_node = new TreeNode(0, "Hello World this node is extra wide to make sure you have to scoll the scene. Is this wide enough yet?"); | |
addItem(d_node); | |
PopupItem *d_popupItem = new PopupItem(0, "This is the tooltip"); | |
d_popupItem->setVisible(false); | |
d_popupItem->setZValue(1.0); | |
d_node->setPopupItem(d_popupItem); | |
addItem(d_popupItem); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DACTTREESCENE_H | |
#define DACTTREESCENE_H | |
#include <QGraphicsScene> | |
class TreeNode; | |
class PopupItem; | |
class DactTreeScene : public QGraphicsScene | |
{ | |
Q_OBJECT | |
public: | |
DactTreeScene(QObject *parent = 0); | |
private: | |
TreeNode *d_node; | |
PopupItem *d_popupItem; | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QDebug> | |
#include <QWheelEvent> | |
#include "DactTreeView.hh" | |
#include "DactTreeScene.hh" | |
#include "TreeNode.hh" | |
DactTreeView::DactTreeView(QWidget* parent) | |
: | |
QGraphicsView(parent) | |
{ | |
DactTreeScene *scene = new DactTreeScene(this); | |
setScene(scene); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DACTTREEVIEW_H | |
#define DACTTREEVIEW_H | |
#include <QGraphicsView> | |
class DactTreeScene; | |
class DactTreeView : public QGraphicsView | |
{ | |
Q_OBJECT | |
public: | |
DactTreeView(QWidget *parent = 0); | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <iostream> | |
#include <stdexcept> | |
#include "DactApplication.hh" | |
int main(int argc, char *argv[]) | |
{ | |
int r; | |
try { | |
QCoreApplication::setOrganizationName("RUG"); | |
QCoreApplication::setOrganizationDomain("rug.nl"); | |
QCoreApplication::setApplicationName("Dact"); | |
QScopedPointer<DactApplication> a(new DactApplication(argc, argv)); | |
a->init(); | |
r = a->exec(); | |
} catch (std::logic_error const &e) { | |
std::cerr << e.what() << std::endl; | |
r = 1; | |
} | |
return r; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QMainWindow> | |
#include <QSharedPointer> | |
#include "MainWindow.hh" | |
#include "ui_MainWindow.h" | |
MainWindow::MainWindow(QWidget *parent) : | |
QMainWindow(parent), | |
d_ui(QSharedPointer<Ui::MainWindow>(new Ui::MainWindow)) | |
{ | |
d_ui->setupUi(this); | |
} | |
MainWindow::~MainWindow() | |
{ | |
// | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MAINWINDOW_H | |
#define MAINWINDOW_H | |
#include <QMainWindow> | |
#include <QSharedPointer> | |
namespace Ui { | |
class MainWindow; | |
} | |
class DactTreeScene; | |
class MainWindow : public QMainWindow { | |
Q_OBJECT | |
public: | |
MainWindow(QWidget *parent = 0); | |
~MainWindow(); | |
signals: | |
void corpusReaderCreated(); | |
void queryCancelRequest(); | |
void exportProgressMaximum(int max); | |
void exportProgress(int progress); | |
void exportError(QString const &error); | |
void openError(QString const &error); | |
private: | |
QSharedPointer<Ui::MainWindow> d_ui; | |
}; | |
#endif // MAINWINDOW_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ui version="4.0"> | |
<class>MainWindow</class> | |
<widget class="QMainWindow" name="MainWindow"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>400</width> | |
<height>400</height> | |
</rect> | |
</property> | |
<property name="windowTitle"> | |
<string>Dact</string> | |
</property> | |
<widget class="DactTreeView" name="treeGraphicsView" native="true"/> | |
</widget> | |
<layoutdefault spacing="6" margin="11"/> | |
<customwidgets> | |
<customwidget> | |
<class>DactTreeView</class> | |
<extends>QWidget</extends> | |
<header>DactTreeView.hh</header> | |
</customwidget> | |
</customwidgets> | |
<resources/> | |
<connections/> | |
</ui> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QFont> | |
#include <QPainter> | |
#include <QGraphicsScene> | |
#include <QGraphicsView> | |
#include "PopupItem.hh" | |
#include "TreeNode.hh" | |
PopupItem::PopupItem(QGraphicsItem *parent, QString const &label) | |
: | |
QGraphicsItem(parent), | |
d_label(label), | |
d_padding(5.0) | |
{} | |
QRectF PopupItem::boundingRect() const | |
{ | |
return rect(); | |
} | |
QRectF PopupItem::rect() const | |
{ | |
QSizeF popup = size(); | |
// Center it around the point (the cursor) | |
return QRectF(QPointF(-popup.width() / 2.0, -popup.height() / 2.0), popup); | |
} | |
QFont PopupItem::font() const | |
{ | |
return QFont("verdana", 12); | |
} | |
void PopupItem::paint(QPainter *painter, QStyleOptionGraphicsItem const *option, | |
QWidget *widget) | |
{ | |
// Get the font size. | |
QFont painterFont(font()); | |
// Bounding box | |
QRectF bRect(rect()); | |
// Inner text box | |
QRectF textRect(bRect.x() + d_padding, bRect.y() + d_padding, | |
bRect.width() - 2.0 * d_padding, bRect.height() - 2.0 * d_padding); | |
// Popup box | |
painter->setRenderHint(QPainter::Antialiasing, true); | |
painter->setOpacity(0.9); | |
painter->setBrush(QBrush(Qt::gray)); | |
painter->setPen(QPen(QBrush(), 0.0)); | |
painter->drawRoundedRect(bRect, 5.0, 5.0); | |
// Popup text | |
painter->setOpacity(1.0); | |
painter->setBrush(QBrush()); | |
painter->setPen(QPen()); | |
painter->setFont(painterFont); | |
painter->drawText(textRect, Qt::AlignCenter, d_label); | |
} | |
QSizeF PopupItem::size() const | |
{ | |
QFontMetricsF metrics(font()); | |
return QSizeF( | |
metrics.width(d_label) + 2.0 * d_padding, | |
metrics.lineSpacing() + 2.0 * d_padding); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef POPUPITEM_HH | |
#define POPUPITEM_HH | |
#include <QGraphicsItem> | |
#include <QString> | |
class PopupItem : public QGraphicsItem | |
{ | |
public: | |
PopupItem(QGraphicsItem *parent, QString const &label); | |
QRectF boundingRect() const; | |
QFont font() const; | |
void paint(QPainter *painter, QStyleOptionGraphicsItem const *option, QWidget *widget); | |
QSizeF size() const; | |
QRectF rect() const; | |
private: | |
QString d_label; | |
qreal d_padding; | |
}; | |
#endif | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QGraphicsScene> | |
#include <QGraphicsSceneHoverEvent> | |
#include <QGraphicsView> | |
#include <QPainter> | |
#include "TreeNode.hh" | |
#include "PopupItem.hh" | |
TreeNode::TreeNode(QGraphicsItem *parent, QString const &label) : | |
QGraphicsItem(parent), | |
d_label(label), | |
d_popupItem(0), | |
d_leafPadding(10) | |
{ | |
setFlags(ItemIsSelectable | ItemIsFocusable); | |
setAcceptHoverEvents(true); | |
} | |
void TreeNode::hoverEnterEvent(QGraphicsSceneHoverEvent *event) | |
{ | |
if (d_popupItem) | |
{ | |
d_popupItem->setPos(event->scenePos()); | |
d_popupItem->setVisible(true); | |
event->accept(); | |
} | |
} | |
void TreeNode::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | |
{ | |
if (d_popupItem && d_popupItem->isVisible()) | |
{ | |
d_popupItem->setVisible(false); | |
event->accept(); | |
} | |
} | |
void TreeNode::hoverMoveEvent(QGraphicsSceneHoverEvent *event) | |
{ | |
if (d_popupItem && d_popupItem->isVisible()) | |
{ | |
d_popupItem->setPos(event->scenePos()); | |
event->accept(); | |
} | |
} | |
QRectF TreeNode::boundingRect() const | |
{ | |
return leafRect(); | |
} | |
QPainterPath TreeNode::shape() const | |
{ | |
QPainterPath path; | |
path.addEllipse(leafRect()); | |
return path; | |
} | |
QRectF TreeNode::leafRect() const | |
{ | |
return QRectF(QPointF(0, 0), leafSize()); | |
} | |
QSizeF TreeNode::leafSize() const | |
{ | |
QFontMetricsF metrics(font()); | |
return QSizeF( | |
metrics.width(d_label) + 2 * d_leafPadding, | |
metrics.lineSpacing() + 2 * d_leafPadding); | |
} | |
QFont TreeNode::font() const | |
{ | |
return QFont("verdana", 12); | |
} | |
void TreeNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
{ | |
QRectF leaf(leafRect()); | |
QRectF text( | |
QPointF(d_leafPadding, d_leafPadding), | |
QSizeF( | |
leaf.width() - 2 * d_leafPadding, | |
leaf.height() - 2 * d_leafPadding)); | |
painter->setRenderHint(QPainter::Antialiasing, true); | |
painter->fillRect(leaf, QColor(Qt::white)); | |
QPen borderPen(Qt::black, 1); | |
painter->setPen(borderPen); | |
painter->drawRect(leaf); | |
painter->setPen(QColor(Qt::black)); | |
painter->setFont(font()); | |
painter->drawText(text, Qt::AlignCenter, d_label); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TREENODE_HH | |
#define TREENODE_HH | |
#include <QGraphicsItem> | |
#include <QList> | |
class PopupItem; | |
class TreeNode : public QGraphicsItem | |
{ | |
public: | |
TreeNode(QGraphicsItem *parent, QString const &label); | |
QRectF boundingRect() const; | |
QRectF leafRect() const; | |
QSizeF leafSize() const; | |
void paint(QPainter *painter, QStyleOptionGraphicsItem const *option, QWidget *widget); | |
void setPopupItem(PopupItem *item); | |
QPainterPath shape() const; | |
protected: | |
void hoverEnterEvent(QGraphicsSceneHoverEvent * event); | |
void hoverLeaveEvent(QGraphicsSceneHoverEvent * event); | |
void hoverMoveEvent(QGraphicsSceneHoverEvent * event); | |
private: | |
QFont font() const; | |
QString d_label; | |
PopupItem *d_popupItem; | |
qreal d_leafPadding; | |
}; | |
inline void TreeNode::setPopupItem(PopupItem *item) | |
{ | |
d_popupItem = item; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment