File: pqPythonTabWidget.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 (449 lines) | stat: -rw-r--r-- 13,019 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
// SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
// SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
// SPDX-License-Identifier: BSD-3-Clause

#include "pqPythonTabWidget.h"

#include "pqClickableLabel.h"
#include "pqCoreUtilities.h"
#include "pqPythonEditorActions.h"
#include "pqPythonScriptEditor.h"
#include "pqPythonTextArea.h"
#include "pqTextLinker.h"

#include <QKeyEvent>
#include <QLabel>
#include <QMenu>
#include <QPushButton>
#include <QStandardPaths>
#include <QTabBar>
#include <QTextStream>

//-----------------------------------------------------------------------------
pqPythonTabWidget::pqPythonTabWidget(QWidget* parent)
  : QTabWidget(parent)
{
  this->addNewTabWidget();
  this->createNewEmptyTab();
  this->setMovable(false);

  this->connect(this, &QTabWidget::tabCloseRequested, [this](int index) {
    pqPythonTextArea* widget = this->getWidget<pqPythonTextArea>(index);
    if (widget)
    {
      // First focus to the tab the user wants to delete
      // and update the QTabBar
      const int widgetId = this->indexOf(widget);
      this->setCurrentIndex(widgetId);
      this->repaint();

      // We stop all display updates
      // to avoid segfaults
      this->setUpdatesEnabled(false);
      if (this->count() == 2)
      {
        // Only delete the widget if it's not empty
        if (widget->isEmpty())
        {
          this->setUpdatesEnabled(true);
          return;
        }

        this->createNewEmptyTab();
      }

      if (widget->saveOnClose())
      {
        if (this->currentIndex() == index)
        {
          this->setCurrentIndex((index + 1) % (this->count() - 1));
          this->lastFocus = this->getWidget<pqPythonTextArea>(this->currentIndex());
        }

        this->removeTab(index);
        widget->deleteLater();
      }
      this->setUpdatesEnabled(true);
    }
  });
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::connectActions(pqPythonEditorActions& actions)
{
  pqPythonEditorActions::connect(actions, this);
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::updateActions(pqPythonEditorActions& actions)
{
  const int lastIdx = this->indexOf(this->lastFocus);
  const int currentIdx = this->currentIndex();

  if (currentIdx == this->count() - 1)
  {
    if (lastIdx == -1)
    {
      this->setCurrentIndex(0);
    }
    else
    {
      this->setCurrentIndex(lastIdx);
    }

    return;
  }

  if (this->lastFocus)
  {
    this->lastFocus->disconnectActions(actions);
  }

  this->lastFocus = this->getCurrentTextArea();
  this->lastFocus->connectActions(actions);
}

//-----------------------------------------------------------------------------
pqPythonTextArea* pqPythonTabWidget::getCurrentTextArea() const
{
  return this->getWidget<pqPythonTextArea>(this->currentIndex());
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::addNewTextArea(const QString& fileName, vtkTypeUInt32 location)
{
  // If the fileName is empty abort
  if (fileName.isEmpty())
  {
    return;
  }

  const int isOpened = this->fileIsOpened(fileName);
  if (isOpened != -1)
  {
    this->setCurrentIndex(isOpened);
    return;
  }

  pqPythonTextArea* tArea = this->getCurrentTextArea();
  if (!tArea->isEmpty() || tArea->isLinked())
  {
    this->createNewEmptyTab();
    tArea = this->getCurrentTextArea();
  }

  // If not create a new tab
  tArea->openFile(fileName, location);
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::loadFile(const QString& fileName)
{
  // If the fileName is empty abort
  if (fileName.isEmpty())
  {
    return;
  }

  QFile file(fileName);
  if (!file.open(QFile::ReadOnly | QFile::Text))
  {
    QMessageBox::warning(pqCoreUtilities::mainWidget(), tr("Sorry!"),
      tr("Cannot open file %1:\n%2.").arg(fileName).arg(file.errorString()));
    return;
  }

  QTextStream in(&file);
  const QString str = in.readAll();

  // If not create a new tab
  pqPythonTextArea* tArea = this->getCurrentTextArea();
  tArea->setText(str);
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::closeCurrentTab()
{
  Q_EMIT this->tabCloseRequested(this->currentIndex());
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::keyPressEvent(QKeyEvent* keyEvent)
{
  if (keyEvent->matches(QKeySequence::AddTab))
  {
    this->createNewEmptyTab();
    keyEvent->accept();
  }
  else if (keyEvent->matches(QKeySequence::NextChild))
  {
    const int nextTabId = (this->currentIndex() + 1) % (this->count() - 1);
    this->setCurrentIndex(nextTabId);
  }
  else if (keyEvent->matches(QKeySequence::PreviousChild))
  {
    const int nextTabId = (this->currentIndex() + this->count() - 2) % (this->count() - 1);
    this->setCurrentIndex(nextTabId);
  }

  keyEvent->ignore();
}

void pqPythonTabWidget::mousePressEvent(QMouseEvent* mouseEvent)
{
  if (mouseEvent->button() == Qt::RightButton)
  {
    const QPoint& mousePosition = mouseEvent->pos();
    const int tabIndex = this->tabBar()->tabAt(mousePosition);

    if (tabIndex >= 0 && tabIndex < this->count())
    {
      QMenu contextMenu;

      QAction closeTabAction;
      closeTabAction.setText(tr("Close"));
      this->connect(&closeTabAction, &QAction::triggered,
        [this, tabIndex]() { this->tabCloseRequested(tabIndex); });
      contextMenu.addAction(&closeTabAction);

      contextMenu.exec(mouseEvent->globalPos());
    }

    mouseEvent->accept();
  }

  mouseEvent->ignore();
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::updateTab(QWidget* widget)
{
  const int idx = this->indexOf(widget);
  if (idx != -1)
  {
    QString tabName;
    QString elidedTabName;
    QString unstyledTabName;
    this->generateTabName(
      qobject_cast<pqPythonTextArea*>(widget), tabName, elidedTabName, unstyledTabName);
    pqClickableLabel* cLabel =
      new pqClickableLabel(widget, elidedTabName, tabName, unstyledTabName, nullptr, widget);
    this->tabBar()->setTabButton(idx, QTabBar::LeftSide, cLabel);
    this->connect(cLabel, &pqClickableLabel::onClicked, [this](QWidget* clickedWidget) {
      int tmpIdx = this->indexOf(clickedWidget);
      if (tmpIdx >= 0 && tmpIdx < this->count() - 1)
      {
        this->setCurrentIndex(tmpIdx);
      }
    });
  }
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::createNewEmptyTab()
{
  pqPythonTextArea* widget = new pqPythonTextArea(this);

  this->insertTab(this->count() - 1, widget, "");
  this->setCurrentIndex(this->indexOf(widget));
  this->lastFocus = widget;

  this->updateTab(widget);

  this->connect(widget, &pqPythonTextArea::fileOpened,
    [this, widget](const QString&) { this->updateTab(widget); });

  this->connect(widget, &pqPythonTextArea::fileSaved,
    [this, widget](const QString&) { this->updateTab(widget); });

  this->connect(
    widget, &pqPythonTextArea::contentChanged, [this, widget]() { this->updateTab(widget); });

  this->connect(widget, &pqPythonTextArea::fileOpened, this, &pqPythonTabWidget::fileOpened);
  this->connect(widget, &pqPythonTextArea::fileSaved, this, &pqPythonTabWidget::fileSaved);
  this->setTabCloseButton(widget);
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::setTabCloseButton(pqPythonTextArea* widget)
{
  QPixmap closePixmap = this->style()->standardIcon(QStyle::SP_TitleBarCloseButton).pixmap(16, 16);
  QString label = tr("Close Tab");
  pqClickableLabel* cLabel =
    new pqClickableLabel(widget, label, label, label, &closePixmap, widget);
  this->tabBar()->setTabButton(this->count() - 2, QTabBar::RightSide, cLabel);
  this->connect(cLabel, &pqClickableLabel::onClicked, [this](QWidget* clickedWidget) {
    int idx = this->indexOf(clickedWidget);
    if (idx >= 0 && idx < this->count() - 1)
    {
      Q_EMIT this->tabCloseRequested(idx);
    }
  });
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::addNewTabWidget()
{
  QWidget* newTabWidget = new QWidget(this);
  this->addTab(newTabWidget, "+");

  // New tab widget is always the last one
  this->connect(this, &QTabWidget::tabBarClicked, [this](int index) {
    if (index == this->count() - 1)
    {
      this->createNewEmptyTab();
    }
  });
}

//-----------------------------------------------------------------------------
bool pqPythonTabWidget::saveOnClose()
{
  bool rValue = true;
  for (int i = 0; i < this->count() - 1; ++i)
  {
    pqPythonTextArea* widget = this->getWidget<pqPythonTextArea>(i);
    if (widget)
    {
      rValue &= widget->saveOnClose();
    }
  }

  return rValue;
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::createParaviewTraceTab()
{
  if (!this->TraceWidget)
  {
    pqPythonTextArea* widget = this->getCurrentTextArea();
    if (!widget->isEmpty() || widget->isLinked())
    {
      this->createNewEmptyTab();
      widget = this->getCurrentTextArea();
    }
    this->TraceWidget = widget;
  }
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::updateTrace(const QString& str)
{
  this->createParaviewTraceTab();

  const int traceWidgetId = this->indexOf(this->TraceWidget);
  this->tabBar()->setCurrentIndex(traceWidgetId);
  this->TraceWidget->setText(str);
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::stopTrace(const QString& str)
{
  this->createParaviewTraceTab();

  const int traceWidgetId = this->indexOf(this->TraceWidget);
  this->tabBar()->setCurrentIndex(traceWidgetId);
  this->TraceWidget->setText(str);
  this->TraceWidget = nullptr;
  this->updateTab(this->widget(traceWidgetId));
}

//-----------------------------------------------------------------------------
void pqPythonTabWidget::generateTabName(const pqPythonTextArea* widget, QString& tabName,
  QString& elidedTabName, QString& unstyledTabName) const
{
  const auto QColorToString = [](const QColor& color) { return color.name(QColor::HexRgb); };
  const auto ColorText = [&QColorToString](const QColor& color, const QString& text) {
    return "<span style=\"color:" + QColorToString(color) + "\">" + text + "</span>";
  };

  QString fileName = widget->getFilename();
  const QFileInfo fileInfo(fileName);
  const QString macroDir = pqPythonScriptEditor::getMacrosDir();
  const QString scriptDir = pqPythonScriptEditor::getScriptsDir();

  // Insert a tag for the tab type
  QString prefix;
  QColor prefixColor;
  if (widget == this->TraceWidget)
  {
    prefix = "[Trace]";
    prefixColor = Qt::GlobalColor::darkCyan;
  }
  else if (fileName.contains(macroDir))
  {
    prefix = "[Macro]";
    prefixColor = Qt::GlobalColor::darkGreen;
  }
  else if (fileName.contains(scriptDir))
  {
    prefix = "[Script]";
    prefixColor = Qt::GlobalColor::darkBlue;
  }
  tabName = ColorText(prefixColor, prefix);
  unstyledTabName = prefix;

  if (widget->isLinked())
  {
    tabName += ColorText(Qt::GlobalColor::darkYellow, "[Linked]");
    unstyledTabName += "[Linked]";
  }

  tabName += " ";
  unstyledTabName += " ";

  if (fileName.isEmpty())
  {
    tabName += tr("New File");
    elidedTabName = tabName;
    unstyledTabName += tr("New File");
  }
  else
  {
    QString absoluteDirPath = fileInfo.dir().absolutePath();

    // Replace the home folder path with ~
    const QString homeDirectory = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    absoluteDirPath.replace(homeDirectory, "~");

    elidedTabName = tabName;
    tabName += ColorText(Qt::GlobalColor::gray, absoluteDirPath) + "/";
    unstyledTabName += absoluteDirPath + "/";

    // Path can get long, elide it
    QLabel tmpLabel;
    QFontMetrics metrics(tmpLabel.font());
    QString elidedAbsoluteDirPath = metrics.elidedText(absoluteDirPath, Qt::ElideMiddle, 200);
    elidedTabName += ColorText(Qt::GlobalColor::gray, elidedAbsoluteDirPath) + "/";

    QString styledFileName;
    if (widget->isDirty())
    {
      styledFileName = "<b>" + ColorText(Qt::GlobalColor::red, fileInfo.fileName()) + "</b>";
    }
    else
    {
      styledFileName = fileInfo.fileName();
    }
    tabName += styledFileName;
    unstyledTabName += styledFileName;
    elidedTabName += styledFileName;
  }
}

//-----------------------------------------------------------------------------
int pqPythonTabWidget::fileIsOpened(const QString& fileName) const
{
  const QFileInfo fileInfo(fileName);
  for (int i = 0; i < this->count() - 1; ++i)
  {
    pqPythonTextArea* widget = this->getWidget<pqPythonTextArea>(i);
    if (widget && QFileInfo(widget->getFilename()) == fileInfo)
    {
      return i;
    }
  }

  return -1;
}