File: pqPythonMacroSupervisor.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (590 lines) | stat: -rw-r--r-- 18,002 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
143
144
145
146
147
148
149
150
151
152
153
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
// SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
// SPDX-License-Identifier: BSD-3-Clause
#include "pqPythonMacroSupervisor.h"
#include "pqApplicationCore.h"
#include "pqIconSettings.h"
#include "pqPythonMacroSettings.h"
#include "pqServer.h"

#include "pqCoreUtilities.h"
#include "pqIconListModel.h"
#include "pqPythonManager.h"
#include "vtksys/SystemTools.hxx"

#include <QAction>
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QMenu>
#include <QPointer>
#include <QSet>
#include <QString>
#include <QStringList>
#include <QVariant>
#include <QWidget>

#if defined(_WIN32) && !defined(__CYGWIN__)
const char ENV_PATH_SEP = ';';
#else
const char ENV_PATH_SEP = ':';
#endif

class pqPythonMacroSupervisor::pqInternal
{
public:
  // Container widget that have an RunMacro action context
  QList<QPointer<QWidget>> RunWidgetContainers;
  // List of action linked to widget/menuItem used to start a macro
  QMap<QString, QAction*> RunActionMap;

  // Container widget that have an EditMacro action context
  QList<QPointer<QWidget>> EditWidgetContainers;
  // List of action linked to widget/menuItem used to edit a macro
  QMap<QString, QAction*> EditActionMap;

  // Container widget that have an DeleteMacro action context
  QList<QPointer<QWidget>> DeleteWidgetContainers;
  // List of action linked to widget/menuItem used to delete a macro
  QMap<QString, QPointer<QAction>> DeleteActionMap;

  static pqIconSettings IconSettings() { return pqIconSettings("Macros"); }
  static pqPythonMacroSettings MacroSettings(pqPythonMacroSettings::MacroItemCategories category)
  {
    return pqPythonMacroSettings(category);
  }

  /**
   * Find an icon for given macro, looking under standard macro directories.
   * Matching icon has same base name than the macro and ends with one of the
   * supported icon extension.
   * Return an empty string if no such a file exist.
   */
  static QString findMacroIconUnderMacroDir(const QString& macroPath)
  {
    // keep looking in macro dir for backward compatibility.
    QStringList iconDirs = pqCoreUtilities::findParaviewPaths(QString("Macros"), true, true);

    auto fileBaseName = QFileInfo(macroPath).completeBaseName();
    QStringList filter;
    for (auto extension : pqIconListModel::getSupportedIconFormats())
    {
      filter << fileBaseName + extension;
    }

    for (const auto& iconDir : iconDirs)
    {
      auto directory = QDir(iconDir);
      auto icons = directory.entryInfoList(filter, QDir::Files);
      if (!icons.empty())
      {
        return icons.first().absoluteFilePath();
      }
    }

    return QString();
  }
};

//----------------------------------------------------------------------------
pqPythonMacroSupervisor::pqPythonMacroSupervisor(QObject* p)
  : QObject(p)
{
  this->Internal = new pqInternal;
  QObject::connect(pqApplicationCore::instance(), SIGNAL(updateMasterEnableState(bool)), this,
    SLOT(updateMacroList()));
}

//----------------------------------------------------------------------------
pqPythonMacroSupervisor::~pqPythonMacroSupervisor()
{
  delete this->Internal;
  this->Internal = nullptr;
}

// Util methods
//----------------------------------------------------------------------------
namespace
{
void addPlaceHolderIfNeeded(QWidget* widget)
{
  QMenu* menu = qobject_cast<QMenu*>(widget);
  if (menu && menu->isEmpty())
  {
    menu->addAction(QCoreApplication::translate("pqPythonMacroSupervisor", "empty"))->setEnabled(0);
  }
}
void removePlaceHolderIfNeeded(QWidget* widget)
{
  QMenu* menu = qobject_cast<QMenu*>(widget);
  if (menu && menu->actions().size() == 1)
  {
    QAction* act = menu->actions()[0];
    // It's a placeholder if it is disabled and has empty data().
    if (!act->isEnabled() && act->data().toString().length() == 0)
    {
      menu->removeAction(act);
      delete act;
    }
  }
}
void addActionToWidgets(QAction* action, QList<QPointer<QWidget>>& widgets)
{
  Q_FOREACH (QWidget* widget, widgets)
  {
    removePlaceHolderIfNeeded(widget);
    if (widget)
    {
      widget->addAction(action);
    }
  }
}
void removeActionFromWidgets(QAction* action, QList<QPointer<QWidget>>& widgets)
{
  Q_FOREACH (QWidget* widget, widgets)
  {
    if (widget)
    {
      widget->removeAction(action);
    }
    addPlaceHolderIfNeeded(widget);
  }
}
}

//----------------------------------------------------------------------------
QAction* pqPythonMacroSupervisor::getMacro(const QString& fileName)
{
  if (this->Internal->RunActionMap.contains(fileName))
  {
    return this->Internal->RunActionMap[fileName];
  }
  return nullptr;
}
//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::addWidgetForRunMacros(QWidget* widget)
{
  this->addWidgetForMacros(widget, 0);
}
//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::addWidgetForEditMacros(QWidget* widget)
{
  this->addWidgetForMacros(widget, 1);
}
//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::addWidgetForDeleteMacros(QWidget* widget)
{
  this->addWidgetForMacros(widget, 2);
}
//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::addWidgetForMacros(QWidget* widget, int actionType)
{
  QList<QPointer<QWidget>>* widgetContainers = nullptr;
  switch (actionType)
  {
    case 0: // run
      widgetContainers = &this->Internal->RunWidgetContainers;
      break;
    case 1: // edit
      widgetContainers = &this->Internal->EditWidgetContainers;
      break;
    case 2: // delete
      widgetContainers = &this->Internal->DeleteWidgetContainers;
      break;
  }

  if (widget && !widgetContainers->contains(widget))
  {
    addPlaceHolderIfNeeded(widget);
    widgetContainers->append(widget);
  }
  this->resetActions();
}
//----------------------------------------------------------------------------
QMap<QString, QString> pqPythonMacroSupervisor::getStoredMacros()
{
  QStringList fileNames = getMacrosFilePaths();

  QMap<QString, QString> macros;
  for (int i = 0; i < fileNames.size(); ++i)
  {
    macros.insert(fileNames[i], macroNameFromFileName(fileNames[i]));
  }
  return macros;
}

//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::hideFile(const QString& fileName)
{
  QFileInfo file = QFileInfo(fileName);
  QDir dir = file.absoluteDir();
  QString baseName = file.baseName();
  QString suffix = file.completeSuffix();

  int index = 1;
  QString pattern = "." + baseName + "%1%2." + suffix;
  QString newName = pattern.arg("").arg("");
  while (dir.exists(newName))
  {
    newName = pattern.arg("-").arg(QString::number(index));
    index++;
  }
  QString newFilePath = dir.absolutePath() + QDir::separator() + newName;
  QFile::rename(fileName, newFilePath);
}
//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::updateMacroList()
{
  this->resetActions();
}

//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::resetActions()
{
  // Delete RUN
  Q_FOREACH (QAction* action, this->Internal->RunActionMap.values())
  {
    removeActionFromWidgets(action, this->Internal->RunWidgetContainers);
    delete action;
  }
  this->Internal->RunActionMap.clear();
  // Delete EDIT
  Q_FOREACH (QAction* action, this->Internal->EditActionMap.values())
  {
    removeActionFromWidgets(action, this->Internal->EditWidgetContainers);
    delete action;
  }
  this->Internal->EditActionMap.clear();
  // Delete DELETE
  Q_FOREACH (QAction* action, this->Internal->DeleteActionMap.values())
  {
    removeActionFromWidgets(action, this->Internal->DeleteWidgetContainers);
    delete action;
  }
  this->Internal->DeleteActionMap.clear();

  // Key is filename, value is macroname
  QMap<QString, QString> macros = pqPythonMacroSupervisor::getStoredMacros();
  QMap<QString, QString>::const_iterator itr;
  for (itr = macros.constBegin(); itr != macros.constEnd(); ++itr)
  {
    this->addMacro(itr.value(), itr.key());
  }
}

//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::addMacro(const QString& fileName)
{
  this->addMacro(pqPythonMacroSupervisor::macroNameFromFileName(fileName), fileName);
}

//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::addMacro(const QString& macroName, const QString& fileName)
{
  this->addMacro(macroName, macroName, fileName);
}

//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::addMacro(
  const QString& macroName, const QString& tip, const QString& fileName)
{
  QAction* action = this->getMacro(fileName);

  // If the macro already exists we just need to update it's name
  if (action)
  {
    action->setText(macroName);
    return;
  }

  // If we are master we allow macros
  bool enable = pqApplicationCore::instance()->getActiveServer()
    ? pqApplicationCore::instance()->getActiveServer()->isMaster()
    : true;

  // Run action
  QAction* runAction = new QAction(macroName, this);
  runAction->setData(fileName);
  runAction->setEnabled(enable);
  runAction->setToolTip(tip);

  QString storedName = pqPythonMacroSupervisor::macroNameFromFileName(fileName);
  if (!storedName.isEmpty())
  {
    runAction->setText(storedName);
  }
  else
  {
    auto settings = pqInternal::MacroSettings(pqPythonMacroSettings::MacroItemCategories::Name);
    settings.removeItemFromSettings(fileName);
  }

  QString macroToolTip = pqPythonMacroSupervisor::macroToolTipFromFileName(fileName);
  if (!macroToolTip.isEmpty())
  {
    runAction->setToolTip(macroToolTip);
  }
  else
  {
    auto settings = pqInternal::MacroSettings(pqPythonMacroSettings::MacroItemCategories::ToolTip);
    settings.removeItemFromSettings(fileName);
  }

  QString iconPath = pqPythonMacroSupervisor::iconPathFromFileName(fileName);
  if (!iconPath.isEmpty() && QFileInfo::exists(iconPath))
  {
    runAction->setIcon(QIcon(iconPath));
  }
  else
  {
    pqInternal::IconSettings().removeItemFromSettings(fileName);
  }

  this->Internal->RunActionMap.insert(fileName, runAction);
  this->connect(runAction, SIGNAL(triggered()), SLOT(onMacroTriggered()));

  // Edit action
  QAction* editAction = new QAction(macroName, this);
  editAction->setData(fileName);
  editAction->setEnabled(enable);
  this->Internal->EditActionMap.insert(fileName, editAction);
  this->connect(editAction, SIGNAL(triggered()), SLOT(onEditMacroTriggered()));

  // Delete action
  QAction* deleteAction = new QAction(macroName, this);
  deleteAction->setData(fileName);
  deleteAction->setEnabled(enable);
  this->Internal->DeleteActionMap.insert(fileName, deleteAction);
  this->connect(deleteAction, SIGNAL(triggered()), SLOT(onDeleteMacroTriggered()));

  // Add action to widgets
  addActionToWidgets(runAction, this->Internal->RunWidgetContainers);
  addActionToWidgets(editAction, this->Internal->EditWidgetContainers);
  addActionToWidgets(deleteAction, this->Internal->DeleteWidgetContainers);

  Q_EMIT this->onAddedMacro();
}
//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::removeMacro(const QString& fileName)
{
  QAction* action = this->getMacro(fileName);
  if (!action)
  {
    return;
  }

  removeActionFromWidgets(action, this->Internal->RunWidgetContainers);
  this->Internal->RunActionMap.remove(fileName);
  delete action;

  action = this->Internal->EditActionMap[fileName];
  removeActionFromWidgets(action, this->Internal->EditWidgetContainers);
  this->Internal->EditActionMap.remove(fileName);
  delete action;

  action = this->Internal->DeleteActionMap[fileName];
  removeActionFromWidgets(action, this->Internal->DeleteWidgetContainers);
  this->Internal->DeleteActionMap.remove(fileName);
  delete action;

  pqInternal::IconSettings().removeItemFromSettings(fileName);
}

//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::onMacroTriggered()
{
  QObject* action = this->sender();
  // Get the filenames before executing the corresponding scripts. (See #18261)
  QList<QString> filenames;
  QMap<QString, QAction*>::const_iterator itr = this->Internal->RunActionMap.constBegin();
  for (; itr != this->Internal->RunActionMap.constEnd(); ++itr)
  {
    if (itr.value() == action)
    {
      filenames.append(itr.key());
    }
  }
  Q_FOREACH (QString filename, filenames)
  {
    Q_EMIT this->executeScriptRequested(filename);
  }
}
//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::onDeleteMacroTriggered()
{
  QObject* action = this->sender();
  QList<QString> listOfMacroToDelete;
  QMap<QString, QPointer<QAction>>::const_iterator itr =
    this->Internal->DeleteActionMap.constBegin();
  for (; itr != this->Internal->DeleteActionMap.constEnd(); ++itr)
  {
    if (itr.value() == action)
    {
      const QString& filename = itr.key();
      listOfMacroToDelete.append(filename);
    }
  }

  for (const QString& filename : listOfMacroToDelete)
  {
    // look for related file with same basename (as icon file)
    QDir dir = QFileInfo(filename).absoluteDir();
    QStringList filter;
    filter << QFileInfo(filename).completeBaseName() + ".py";
    for (auto extension : pqIconListModel::getSupportedIconFormats())
    {
      filter << QFileInfo(filename).completeBaseName() + extension;
    }
    auto fileList = dir.entryInfoList(filter);

    for (auto file : fileList)
    {
      pqPythonMacroSupervisor::hideFile(file.absoluteFilePath());
    }

    this->removeMacro(filename);
  }
}
//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::onEditMacroTriggered()
{
  QObject* action = this->sender();
  QMap<QString, QAction*>::const_iterator itr = this->Internal->EditActionMap.constBegin();
  for (; itr != this->Internal->EditActionMap.constEnd(); ++itr)
  {
    if (itr.value() == action)
    {
      const QString& filename = itr.key();
      Q_EMIT onEditMacro(filename);
    }
  }
}

//----------------------------------------------------------------------------
QString pqPythonMacroSupervisor::macroNameFromFileName(const QString& fileName)
{
  auto settings = pqInternal::MacroSettings(pqPythonMacroSettings::MacroItemCategories::Name);
  QString name = settings.getItemFromSettings(fileName);
  if (name.isEmpty())
  {
    name = QFileInfo(fileName).fileName().replace(".py", "");
    if (!name.length())
    {
      name = "Unnamed macro";
    }
  }
  return name;
}

//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::setNameForMacro(const QString& macroPath, const QString& name)
{
  auto settings = pqInternal::MacroSettings(pqPythonMacroSettings::MacroItemCategories::Name);
  if (name.isEmpty())
  {
    settings.removeItemFromSettings(macroPath);
  }
  else
  {
    settings.setItemInSettings(macroPath, name);
  }
}

//----------------------------------------------------------------------------
QString pqPythonMacroSupervisor::macroToolTipFromFileName(const QString& fileName)
{
  auto settings = pqInternal::MacroSettings(pqPythonMacroSettings::MacroItemCategories::ToolTip);
  QString tooltip = settings.getItemFromSettings(fileName);
  if (!tooltip.isEmpty())
  {
    return tooltip;
  }
  else
  {
    return pqPythonMacroSupervisor::macroNameFromFileName(fileName);
  }
}

//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::setTooltipForMacro(const QString& macroPath, const QString& tooltip)
{
  auto settings = pqInternal::MacroSettings(pqPythonMacroSettings::MacroItemCategories::ToolTip);
  if (tooltip.isEmpty())
  {
    settings.removeItemFromSettings(macroPath);
  }
  else
  {
    settings.setItemInSettings(macroPath, tooltip);
  }
}

//----------------------------------------------------------------------------
void pqPythonMacroSupervisor::setIconForMacro(const QString& macroPath, const QString& iconPath)
{
  auto iconSettings = pqInternal::IconSettings();
  if (iconPath.isEmpty())
  {
    iconSettings.removeItemFromSettings(macroPath);
  }
  else
  {
    iconSettings.setItemIconInSettings(macroPath, iconPath);
  }
}

//----------------------------------------------------------------------------
QString pqPythonMacroSupervisor::iconPathFromFileName(const QString& fileName)
{
  QString iconPath = pqInternal::IconSettings().getIconFromSettings(fileName);
  if (!iconPath.isEmpty())
  {
    return iconPath;
  }

  iconPath = pqInternal::findMacroIconUnderMacroDir(fileName);

  return iconPath;
}

//----------------------------------------------------------------------------
QStringList pqPythonMacroSupervisor::getMacrosFilePaths()
{
  QStringList macroList;
  QDir dir;
  dir.setFilter(QDir::Files);

  QStringList macroDirs = pqCoreUtilities::findParaviewPaths(QString("Macros"), true, true);

  // add user defined macro paths from environment variable PV_MACRO_PATH
  const char* env = vtksys::SystemTools::GetEnv("PV_MACRO_PATH");
  if (env)
  {
    QStringList macroPathDirs = QString::fromUtf8(env).split(ENV_PATH_SEP);
    macroDirs << macroPathDirs;
  }

  Q_FOREACH (QString dirPath, macroDirs)
  {
    if (QFile::exists(dirPath))
    {
      dir.setPath(dirPath);
      for (int i = 0; i < dir.entryList().size(); ++i)
      {
        const QString filePath = dir.entryList().at(i);
        if (filePath.startsWith("."))
        {
          continue;
        }
        // allows only ".py"
        if (filePath.endsWith(".py"))
        {
          macroList.push_back(dirPath + QDir::separator() + filePath);
        }
      }
    }
  }

  return macroList;
}