From 96f17eaf3a1f66d7eefd66e51e2137ea404459f4 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 19 Feb 2012 11:54:26 +0000 Subject: Read entity properties, added entity selection and scaling. --- src/editorwindow.cc | 17 ++++++++++++++++- src/inistream.cc | 27 ++++++++++++--------------- src/inistream.h | 7 ++++--- src/mapentity.cc | 29 +++++++++++++++++++++++++++-- src/mapentity.h | 23 ++++++++++++++++++++++- src/mapwidget.cc | 38 ++++++++++++++++++++++++++++++++++---- src/mapwidget.h | 30 ++++++++++++++++++++++++++++-- src/sidebar.cc | 2 +- 8 files changed, 144 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/editorwindow.cc b/src/editorwindow.cc index 20e8403..3435d43 100644 --- a/src/editorwindow.cc +++ b/src/editorwindow.cc @@ -23,6 +23,7 @@ EditorWindow::EditorWindow(QWidget *parent) : QWidget(parent) { editorwindow_mapwidget = new MapWidget(this); editorwindow_sidebar = new SideBar(this); + connect(editorwindow_mapwidget, SIGNAL(selected(MapEntity *)), editorwindow_sidebar, SLOT(setEntity(MapEntity *))); } void EditorWindow::resizeEvent (QResizeEvent *event) @@ -53,13 +54,14 @@ bool EditorWindow::loadFile(const QString &filename) MapEntity *entity = 0; bool in_entity = false; float x, y, z; + float f; QString str; while (ini.getline(textstream)) { if (ini.got_section()) { - qDebug() << "got section" << ini.section(); + //qDebug() << "got section" << ini.section(); if (ini.got_section("zone")) { in_entity = false; @@ -84,6 +86,7 @@ bool EditorWindow::loadFile(const QString &filename) } else if (ini.got_section("planet")) { in_entity = true; + } else { entity = 0; in_entity = false; @@ -96,10 +99,22 @@ bool EditorWindow::loadFile(const QString &filename) } else if(ini.got_key()) { if (in_entity) { + // read entity properties into the MapWidget instance + if (ini.got_key_vector3f("location" , x, y, z)) { entity->set_location(x, y, z); //qDebug() << "got location " << x << " " << y << " " << z; + + } else if (ini.got_key_string("label", str)) { + entity->set_label(str); + + } else if (ini.got_key_string("name", str)) { + entity->set_name(str); + + } else if (ini.got_key_float("radius", f)) { + entity->set_radius(f); } + } else if (ini.in_section("zone")) { if (ini.got_key_string("name", str)) { diff --git a/src/inistream.cc b/src/inistream.cc index 1e76ab8..c89056f 100644 --- a/src/inistream.cc +++ b/src/inistream.cc @@ -121,9 +121,8 @@ bool IniStream::got_key_string(const char *keylabel, QString &valuestring) if (last_read_was_key && (key_current.compare(keylabel) == 0)) { valuestring = value_current; return true; - } else { - return false; } + return false; } bool IniStream::got_key_vector3f(const char *keylabel, float &x, float &y, float &z) @@ -134,36 +133,34 @@ bool IniStream::got_key_vector3f(const char *keylabel, float &x, float &y, float ts >> x; ts >> y; ts >> z; - return true; - } else { - return false; + return true; } + return false; } -/* -bool IniStream::got_key_label(const char * keylabel, QString & labelstring) +bool IniStream::got_key_float(const char *keylabel, float & f) { if (last_read_was_key && (key_current.compare(keylabel) == 0)) { - labelstring = value_current.trimmed(); + QTextStream ts(&value_current, QIODevice::ReadOnly); + f = 0; + ts >> f; return true; - } else { - return false; } + return false; } -bool IniStream::got_key_float(const char * keylabel, float & f) +/* +bool IniStream::got_key_label(const char * keylabel, QString & labelstring) { if (last_read_was_key && (key_current.compare(keylabel) == 0)) { - std::istringstream is(value_current); - if (!(is >> f)) { - f = 0; - } + labelstring = value_current.trimmed(); return true; } else { return false; } } + bool IniStream::got_key_long(const char * keylabel, long & l) { if (last_read_was_key && (key_current.compare(keylabel) == 0)) { diff --git a/src/inistream.h b/src/inistream.h index 724c598..a9e2817 100644 --- a/src/inistream.h +++ b/src/inistream.h @@ -64,20 +64,21 @@ public: bool got_key(const char * keylabel); - /// check if the last read key=value pair matches keylabel and store the value in valuestring + /// check if the last read key=value pair matches keylabel and store the string value bool got_key_string(const char * keylabel, QString & valuestring); /// check if the last read key=value pair matches keylabel and store the value in x y and z bool got_key_vector3f(const char *keylabel, float &x, float &y, float &z); + /// check if the last read key=value pair matches keylabel and store the float value + bool got_key_float(const char * keylabel, float & f); + /* /// check if the last read key=value pair matches keylabel and store the value in valuestring, converted to label bool got_key_label(const char * keylabel, QString & labelstring); bool got_key_color(const char * keylabel, QColor & color); - bool got_key_float(const char * keylabel, float & f); - bool got_key_angle(const char * keylabel, float & f); bool got_key_long(const char * keylabel, long & l); diff --git a/src/mapentity.cc b/src/mapentity.cc index 516b0b6..dd16379 100644 --- a/src/mapentity.cc +++ b/src/mapentity.cc @@ -8,12 +8,23 @@ #include #include +#include +#include namespace editor { MapEntity::MapEntity(QWidget *parent) : QWidget(parent) { + is_selected = false; + + entity_radius = 0; +} + +void MapEntity::set_selected(const bool selected) +{ + is_selected = selected; + update(); } void MapEntity::set_label(const QString &label) @@ -40,12 +51,26 @@ void MapEntity::set_location(const float x, const float y, const float z) } void MapEntity::paintEvent(QPaintEvent *event) -{ +{ QPen pen(Qt::black, 1, Qt::SolidLine); QPainter painter(this); + if (is_selected) { + pen.setColor(Qt::red); + } + painter.setPen(pen); - painter.drawRect(0, 0, width() - 1 , height() - 1); + painter.drawEllipse(0, 0, width() - 1 , height() - 1); +} + +void MapEntity::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + qDebug() << "clicked entity " << name(); + event->accept(); + + emit clicked(this); + } } } diff --git a/src/mapentity.h b/src/mapentity.h index 788e02d..f08f1b0 100644 --- a/src/mapentity.h +++ b/src/mapentity.h @@ -52,6 +52,12 @@ public: inline const float location(int index) const { return entity_location[index]; } + +signals: + /** + * @brief the clicked() signal is emitted if the entity is clicked with the left mouse button + * */ + void clicked(MapEntity *entity); public slots: @@ -74,11 +80,26 @@ public slots: * @brief set the entity location * */ void set_location(const float x, const float y, const float z); + + /** + * @brief set the selected state + * */ + void set_selected(const bool selected); protected: + /** + * @brief handle draw events + * */ virtual void paintEvent(QPaintEvent *event); + /** + * @brief handle mouse button press events + * */ + virtual void mousePressEvent(QMouseEvent *event); + private: + bool is_selected; + QString entity_label; QString entity_name; QString entity_type; @@ -91,4 +112,4 @@ private: } -#endif // __INCLUDED_EDITOR_MAPENTITY__ \ No newline at end of file +#endif // __INCLUDED_EDITOR_MAPENTITY__ diff --git a/src/mapwidget.cc b/src/mapwidget.cc index 94bed61..a33cf65 100644 --- a/src/mapwidget.cc +++ b/src/mapwidget.cc @@ -9,8 +9,10 @@ #include "mapentity.h" #include +#include #include #include +#include namespace editor { @@ -21,6 +23,8 @@ MapWidget::MapWidget(QWidget *parent) : QWidget(parent) // zoom sucks but it works mapwidget_zoom = 16; + + // center of the map (used for dragging) } void MapWidget::wheelEvent(QWheelEvent *event) @@ -45,16 +49,39 @@ void MapWidget::resizeChildren() for (int i = 0; i < mapwidget_enties.size(); ++i) { MapEntity * entity = mapwidget_enties.at(i); - int iconsize = 12; - int x = width() / 2 - (int) (entity->location(1) * scale) - iconsize /2; - int y = height() / 2 - (int)(entity->location(0) * scale) - iconsize /2; + int radius = (int) (entity->radius() * scale); + if (radius < 6) + radius = 6; + + int x = width() / 2 - (int) (entity->location(1) * scale) - radius; + int y = height() / 2 - (int)(entity->location(0) * scale) - radius; - entity->setGeometry(x, y, iconsize, iconsize); + entity->setGeometry(x, y, radius * 2 , radius * 2); //qDebug() << "Moving entity to " << x << " " << y; } } +void MapWidget::select(MapEntity *entity) +{ + for (int i = 0; i < mapwidget_enties.size(); ++i) { + + MapEntity *mapentity = mapwidget_enties.at(i); + + if (entity == mapentity) { + + entity->set_selected(true); + qDebug() << "selected entity " << entity->name(); + emit selected(entity); + + } else { + + mapentity->set_selected(false); + } + } + update(); +} + void MapWidget::resizeEvent (QResizeEvent *event) { resizeChildren(); @@ -102,6 +129,9 @@ MapEntity *MapWidget::addEntity() MapEntity *mapentity = new MapEntity(this); mapwidget_enties.append(mapentity); + // connect the MapEntity::clicked() signal to the MapWidget::selected() slot + connect(mapentity, SIGNAL(clicked(MapEntity *)), this, SLOT(select(MapEntity *))); + mapentity->show(); resizeChildren(); diff --git a/src/mapwidget.h b/src/mapwidget.h index f698a1c..7371ffd 100644 --- a/src/mapwidget.h +++ b/src/mapwidget.h @@ -32,18 +32,44 @@ public: MapEntity *addEntity(); protected: - virtual void resizeEvent (QResizeEvent *event); + + /** + * @brief handle draw events + * */ virtual void paintEvent(QPaintEvent *event); + + /** + * @brief handle resize events + * */ + virtual void resizeEvent(QResizeEvent *event); + + /** + * @brief handle mousewheel events + * */ virtual void wheelEvent(QWheelEvent *event); + +signals: + /** + * @brief the selected() signal is emitted if an entity on the map is selected + * */ + void selected(MapEntity *entity); + +private slots: + /** + * @brief called when an entity on the map has been clicked + * */ + void select(MapEntity *entity); private: void resizeChildren(); int mapwidget_zoom; + int center_x; + int center_y; QList mapwidget_enties; }; } -#endif // __INCLUDED_EDITOR_MAPWIDGET__ \ No newline at end of file +#endif // __INCLUDED_EDITOR_MAPWIDGET__ diff --git a/src/sidebar.cc b/src/sidebar.cc index 0a99fd8..1c8014f 100644 --- a/src/sidebar.cc +++ b/src/sidebar.cc @@ -51,7 +51,7 @@ SideBar::SideBar(QWidget *parent) : QWidget(parent) void SideBar::setEntity(MapEntity *entity) { - if (entity) { + if (!entity) { edit_entitylabel->setEnabled(false); edit_entitylabel->clear(); -- cgit v1.2.3