Author: mir3x
Date: Thu Nov 12 12:28:52 2015
New Revision: 30563

URL: http://svn.gna.org/viewcvs/freeciv?rev=30563&view=rev
Log:
Added tooltips for chosen item in production widget in city dialog in qt-client.

See patch #6555


Modified:
    branches/S2_6/client/gui-qt/citydlg.cpp
    branches/S2_6/client/gui-qt/citydlg.h
    branches/S2_6/client/gui-qt/optiondlg.cpp
    branches/S2_6/client/gui-qt/optiondlg.h

Modified: branches/S2_6/client/gui-qt/citydlg.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/citydlg.cpp?rev=30563&r1=30562&r2=30563&view=diff
==============================================================================
--- branches/S2_6/client/gui-qt/citydlg.cpp     (original)
+++ branches/S2_6/client/gui-qt/citydlg.cpp     Thu Nov 12 12:28:52 2015
@@ -33,7 +33,9 @@
 #include "climisc.h"
 #include "control.h"
 #include "global_worklist.h"
+#include "helpdata.h"
 #include "mapview_common.h"
+#include "movement.h"
 #include "sprite.h"
 #include "text.h"
 #include "tilespec.h"
@@ -56,6 +58,7 @@
                                        * once per client
                                        */
 static city_dialog *city_dlg;
+extern QString split_text(QString text);
 
 /****************************************************************************
   Draws X on pixmap pointing its useless
@@ -2586,6 +2589,96 @@
   return false;
 }
 
+/**************************************************************************
+  Event filter for catching tooltip events
+**************************************************************************/
+bool fc_tooltip::eventFilter(QObject *obj, QEvent *ev)
+{
+  QHelpEvent *help_event;
+  QString item_tooltip;
+  QRect rect;
+
+  if (ev->type() == QEvent::ToolTip) {
+    QAbstractItemView *view = qobject_cast<QAbstractItemView *>(obj->parent());
+    if (!view) {
+      return false;
+    }
+
+    help_event = static_cast<QHelpEvent *>(ev);
+    QPoint pos = help_event->pos();
+    QModelIndex index = view->indexAt(pos);
+    if (!index.isValid()) {
+      return false;
+    }
+    item_tooltip = view->model()->data(index, Qt::ToolTipRole).toString();
+    rect = view->visualRect(index);
+
+    if (!item_tooltip.isEmpty()) {
+      QToolTip::showText(help_event->globalPos(), item_tooltip, view, rect);
+    } else {
+      QToolTip::hideText();
+    }
+    return true;
+  }
+  return false;
+}
+
+/**************************************************************************
+  Returns shortened help for given universal ( stored in qvar )
+**************************************************************************/
+QString get_tooltip(QVariant qvar)
+{
+  QString str, def_str, ret_str;
+  QStringList sl;
+  char buffer[8192];
+  char buf2[1];
+
+  buf2[0] = '\0';
+  struct universal *target;
+  target = reinterpret_cast<universal *>(qvar.value<void *>());
+  if (target == NULL) {
+  } else if (VUT_UTYPE == target->kind) {
+    def_str = QString(N_("Attack: %1, Defense: %2, Move: %3\n"))
+              .arg(target->value.utype->attack_strength)
+              .arg(target->value.utype->defense_strength)
+              .arg(QString(move_points_text(target->value.utype->move_rate, 
TRUE)));
+    def_str += QString(N_("Cost: %1, Basic Upkeep: %2\n"))
+               .arg(utype_build_shield_cost(target->value.utype))
+               .arg(helptext_unit_upkeep_str(target->value.utype));
+    def_str += QString(N_("Hitpoints: %1, FirePower: %2, Vision: %3\n\n"))
+               .arg(target->value.utype->hp)
+               .arg(target->value.utype->firepower)
+               .arg((int)sqrt((double)target->value.utype->vision_radius_sq));
+    str = helptext_unit(buffer, sizeof(buffer), client.conn.playing,
+                        buf2, target->value.utype);
+  } else {
+    if (!improvement_has_flag(target->value.building, IF_GOLD)) {
+      def_str = QString(N_("Cost: %1, Upkeep: %2\n\n"))
+                .arg(impr_build_shield_cost(target->value.building))
+                .arg(target->value.building->upkeep);
+    }
+    str = helptext_building(buffer, sizeof(buffer), client.conn.playing,
+                            NULL, target->value.building);
+  }
+
+  /* Remove all lines from help which has '*' in first 3 chars */
+  sl = str.split('\n');
+  foreach (const QString & s, sl) {
+    if (s.count() > 2) {
+      if (s.at(0) != '*' && s.at(1) != '*' && s.at(2) != '*') {
+        ret_str = ret_str + s + '\n';
+      }
+    } else {
+      ret_str = ret_str + s + '\n';
+    }
+  }
+
+  ret_str = split_text(ret_str);
+  ret_str = ret_str.trimmed();
+  ret_str = def_str + ret_str;
+
+  return ret_str;
+}
 
 /***************************************************************************
   City item delegate constructor
@@ -2804,8 +2897,13 @@
   if (!index.isValid()) return QVariant();
   if (index.row() >= 0 && index.row() < rowCount() && index.column() >= 0
       && index.column() < columnCount()
-      && (index.column() + index.row() * 3 < city_target_list.count()))
+      && (index.column() + index.row() * 3 < city_target_list.count())) {
+    if (role == Qt::ToolTipRole) {
+      return get_tooltip(city_target_list[index.row() * 3
+                                          + index.column()]->data());
+    }
     return city_target_list[index.row() * 3 + index.column()]->data();
+  }
   return QVariant();
 }
 
@@ -2864,7 +2962,7 @@
 bool city_production_model::setData(const QModelIndex &index,
                                     const QVariant &value, int role)
 {
-  if (!index.isValid() || role != Qt::DisplayRole)
+  if (!index.isValid() || role != Qt::DisplayRole || role != Qt::ToolTipRole)
     return false;
   if (index.row() >= 0 && index.row() < rowCount() && index.column() >= 0
       && index.column() < columnCount()) {
@@ -2888,6 +2986,7 @@
   QPoint pos, sh;
   int desk_width = QApplication::desktop()->width();
   int desk_height = QApplication::desktop()->height();
+  fc_tt = new fc_tooltip(this);
   setAttribute(Qt::WA_DeleteOnClose);
   setWindowFlags(Qt::Popup);
   verticalHeader()->setVisible(false);
@@ -2902,6 +3001,7 @@
   c_p_d = new city_production_delegate(sh, this, pw_city);
   setItemDelegate(c_p_d);
   setModel(list_model);
+  viewport()->installEventFilter(fc_tt);
   installEventFilter(this);
   connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection &,
                                                     const QItemSelection &)),
@@ -3043,6 +3143,7 @@
 {
   delete c_p_d;
   delete list_model;
+  viewport()->removeEventFilter(fc_tt);
   removeEventFilter(this);
 }
 

Modified: branches/S2_6/client/gui-qt/citydlg.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/citydlg.h?rev=30563&r1=30562&r2=30563&view=diff
==============================================================================
--- branches/S2_6/client/gui-qt/citydlg.h       (original)
+++ branches/S2_6/client/gui-qt/citydlg.h       Thu Nov 12 12:28:52 2015
@@ -42,6 +42,21 @@
 // Qt
 #include <QProgressBar>
 #include <QTableWidget>
+#include <QToolTip>
+
+QString get_tooltip(QVariant qvar);
+
+
+class fc_tooltip : public QObject
+{
+  Q_OBJECT
+public:
+  explicit fc_tooltip(QObject *parent = NULL): QObject(parent) {}
+
+protected:
+  bool eventFilter(QObject *obj, QEvent *event);
+};
+
 
 /****************************************************************************
   Subclassed QProgressBar to receive clicked signal
@@ -263,6 +278,7 @@
   int when_change;
   int curr_selection;
   bool sh_units;
+  fc_tooltip *fc_tt;
 };
 
 

Modified: branches/S2_6/client/gui-qt/optiondlg.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/optiondlg.cpp?rev=30563&r1=30562&r2=30563&view=diff
==============================================================================
--- branches/S2_6/client/gui-qt/optiondlg.cpp   (original)
+++ branches/S2_6/client/gui-qt/optiondlg.cpp   Thu Nov 12 12:28:52 2015
@@ -51,7 +51,7 @@
 /****************************************************************************
   Splits long text to 80 characters
 ****************************************************************************/
-static QString split_text(QString text)
+QString split_text(QString text)
 {
   QStringList sl;
   QString st, str, result;

Modified: branches/S2_6/client/gui-qt/optiondlg.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/optiondlg.h?rev=30563&r1=30562&r2=30563&view=diff
==============================================================================
--- branches/S2_6/client/gui-qt/optiondlg.h     (original)
+++ branches/S2_6/client/gui-qt/optiondlg.h     Thu Nov 12 12:28:52 2015
@@ -31,7 +31,7 @@
 class QWidget;
 class QString;
 
-
+QString split_text(QString text);
 /****************************************************************************
   Dialog for client/server options
 ****************************************************************************/


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to