pqObjectNaming.cxx 7.59 KB
Newer Older
1 2 3 4 5
/*=========================================================================

   Program: ParaView
   Module:    pqObjectNaming.cxx

6
   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
7 8 9
   All rights reserved.

   ParaView is a free software; you can redistribute it and/or modify it
10
   under the terms of the ParaView license version 1.2. 
11

12
   See License_v1.2.txt for the full ParaView license.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
   A copy of this license can be obtained by contacting
   Kitware Inc.
   28 Corporate Drive
   Clifton Park, NY 12065
   USA

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/

#include "pqObjectNaming.h"

#include <QAbstractItemDelegate>
#include <QAbstractItemModel>
#include <QAction>
#include <QApplication>
#include <QDockWidget>
#include <QFocusFrame>
#include <QHeaderView>
#include <QItemSelectionModel>
#include <QLabel>
#include <QLayout>
#include <QLineEdit>
#include <QMenu>
#include <QMenuBar>
#include <QScrollBar>
#include <QSet>
#include <QSignalMapper>
#include <QStackedWidget>
#include <QTabBar>
#include <QTabWidget>
#include <QToolBar>
#include <QToolButton>
#include <QtDebug>

58 59 60 61 62 63 64

namespace
{
  QString ErrorMessage;
}

/** Returns the name of an object as if it was unnamed.*/
65
static const QString InternalGetNameAsUnnamed(QObject& Object)
66
{
67
  QString result;
68

69 70
  QObjectList siblings;
  if(Object.parent())
71
    {
72 73 74 75 76 77
    siblings = Object.parent()->children();
    }
  else
    {
    QWidgetList widgets = QApplication::topLevelWidgets();
    for(int i = 0; i != widgets.size(); ++i)
78
      {
79
      siblings.push_back(widgets[i]);
80
      }
81
    }
82
      
83 84 85 86 87 88 89 90 91 92 93
  const QString type = Object.metaObject()->className();
  
  // order of top level widgets is not guarenteed
  // we can someone counter that by checking visibility, 
  // as we usually only test visible widgets, we would get the right one
  int invisible_index = 0;
  int visible_index = 0;
  for(int i = 0; i != siblings.size(); ++i)
    {
    QObject* test = siblings[i];
    if(test == &Object)
94
      {
95
      break;
96
      }
97 98 99
    else if(
      type == test->metaObject()->className()
      && test->objectName().isEmpty())
100
      {
101 102
      QWidget* widget = qobject_cast<QWidget*>(test);
      if(widget && widget->isVisible())
103
        {
104
        ++visible_index;
105 106 107
        }
      else
        {
108
        ++invisible_index;
109 110
        }
      }
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    }
    
  int index = invisible_index;
  if(QWidget* const widget = qobject_cast<QWidget*>(&Object))
    {
    if(widget->isVisible())
      {
      result += QString::number(1);
      index = visible_index;
      }
    else
      {
      result += QString::number(0);
      }
    }
  
  result += type + QString::number(index);

  result.replace("/", "|");
  return result;
}

/** Returns the name of an object.  If the object doesn't have an explicit name,
assigns a name as a convenience.  Also replaces problematic characters such as '/'.
*/
static const QString InternalGetName(QObject& Object)
{
  QString result = Object.objectName();

  if(result.isEmpty())
    {
    result = InternalGetNameAsUnnamed(Object);
143 144
    }

145 146 147 148 149
  if(qobject_cast<QApplication*>(&Object))
  {
      result.append("-app");
  }

150 151 152 153
  result.replace("/", "|");
  return result;
}

154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
const QString pqObjectNaming::GetName(QObject& Object)
{
  QString name = InternalGetName(Object);
  if(name.isEmpty())
    {
    qCritical() << "Cannot record event for unnamed object " << &Object;
    return QString();
    }
  
  for(QObject* p = Object.parent(); p; p = p->parent())
    {
    const QString parent_name = InternalGetName(*p);
    
    if(parent_name.isEmpty())
      {
      qCritical() << "Cannot record event for incompletely-named object " << name << " " << &Object << " with parent " << p;
      return QString();
      }
      
    name = parent_name + "/" + name;
    
    if(!p->parent() && !QApplication::topLevelWidgets().contains(qobject_cast<QWidget*>(p)))
      {
      qCritical() << "Object " << p << " is not a top-level widget";
      return QString();
      }
    }

  return name;
}

186
QObject* pqObjectNaming::GetObject(const QString& Name)
187 188
{
  QObject* result = 0;
189
  QObject* lastObject = 0;
190 191
  if(Name.isEmpty())
    {
192
    return 0;
193 194 195
    }

  const QStringList names = Name.split("/");
196 197 198 199 200 201 202 203

  // see if QApplication is the requested object
  QString app_name = InternalGetName(*QApplication::instance());
  if( app_name == Name)
  {
      return QApplication::instance();
  }

204 205 206 207 208
  const QWidgetList top_level_widgets = QApplication::topLevelWidgets();
  for(int i = 0; i != top_level_widgets.size(); ++i)
    {
    QObject* object = top_level_widgets[i];
    const QString name = InternalGetName(*object);
209
    const QString alt_name = InternalGetNameAsUnnamed(*object);
210
    
211
    if(name == names[0] || alt_name == names[0])
212 213
      {
      result = object;
214
      lastObject = object;
215 216 217 218 219 220 221 222 223 224 225 226 227
      break;
      }
    }
    
  for(int j = 1; j < names.size(); ++j)
    {
    const QObjectList& children = result ? result->children() : QObjectList();
    
    result = 0;
    for(int k = 0; k != children.size(); ++k)
      {
      QObject* child = children[k];
      const QString name = InternalGetName(*child);
228
      const QString alt_name = InternalGetNameAsUnnamed(*child);
229
      
230
      if(name == names[j] || alt_name == names[j])
231 232
        {
        result = child;
233
        lastObject = child;
234 235 236
        break;
        }
      }
237 238 239 240 241 242 243 244 245 246

    // if there is a real name, also allow skipping generations
    // in the hierarchy to find a child
    if(result == 0)
      {
      if(!names[j].isEmpty() && names[j].at(0).isDigit())
        {
        result = result->findChild<QObject*>(names[j]);
        }
      }
247 248 249 250 251
    }
    
  if(result)
    return result;
  
252
  ErrorMessage = QString("Couldn't find object %1\n").arg(Name);
253 254
  if(lastObject)
    {
255
    ErrorMessage += QString("Found up to %1\n").arg(
256
                      pqObjectNaming::GetName(*lastObject));
257
    }
258
  bool foundMatch = false;
259 260 261 262 263 264
  if(lastObject)
    {
    QObjectList matches =
      lastObject->findChildren<QObject*>(names[names.size()-1]);
    foreach(QObject* o, matches)
      {
265
      ErrorMessage  += QString("\tPossible match: %1\n").arg(pqObjectNaming::GetName(*o));
266 267 268 269 270 271 272 273 274
      foundMatch = true;
      }
    }
  if (!foundMatch)
    {
    QObjectList matches = lastObject->findChildren<QObject*>();
    foreach(QObject* o, matches)
      {
      ErrorMessage  += QString("\tAvailable widget: %1\n").arg(pqObjectNaming::GetName(*o));
275 276
      }
    }
277 278

  qCritical() << ErrorMessage ;
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  return 0;
}

void pqObjectNaming::DumpHierarchy(QStringList& results)
{
  const QWidgetList widgets = QApplication::topLevelWidgets();
  for(int i = 0; i != widgets.size(); ++i)
    {
    DumpHierarchy(*widgets[i], results);
    }
}

void pqObjectNaming::DumpHierarchy(QObject& object, QStringList& results)
{
  results << GetName(object);
  
  const QObjectList children = object.children();
  for(int i = 0; i != children.size(); ++i)
    {
    DumpHierarchy(*children[i], results);
    }
}
301 302 303 304 305 306

QString pqObjectNaming::lastErrorMessage()
{
  return ErrorMessage;
}