File: pqViewFrame.cxx

package info (click to toggle)
paraview 5.4.1%2Bdfsg4-3.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 218,616 kB
  • sloc: cpp: 2,331,508; ansic: 322,365; python: 111,051; xml: 79,203; tcl: 47,013; yacc: 4,877; java: 4,438; perl: 3,238; sh: 2,920; lex: 1,908; f90: 748; makefile: 273; pascal: 228; objc: 83; fortran: 31
file content (410 lines) | stat: -rw-r--r-- 13,341 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
/*=========================================================================

   Program: ParaView
   Module:  pqViewFrame.cxx

   Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
   All rights reserved.

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

   See License_v1.2.txt for the full ParaView license.
   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 "pqViewFrame.h"
#include "ui_pqViewFrame.h"

#include "pqSetName.h"
#include "pqView.h"
#include <QAction>
#include <QApplication>
#include <QDrag>
#include <QHBoxLayout>
#include <QLabel>
#include <QMenu>
#include <QMimeData>
#include <QMouseEvent>
#include <QPainter>
#include <QStyle>
#include <QToolBar>
#include <QToolButton>
#include <QVBoxLayout>

#if _WIN32
#include "process.h"
#define getpid _getpid
#else
#include "unistd.h"
#endif

const int ICON_SIZE = 12;

class pqViewFrame::pqInternals
{
public:
  Ui::pqViewFrame Ui;
  pqInternals(pqViewFrame* self) { this->Ui.setupUi(self); }
};

//-----------------------------------------------------------------------------
pqViewFrame::pqViewFrame(QWidget* parentObject)
  : Superclass(parentObject)
  , DecorationsVisible(true)
  , TitleBarVisible(true)
  , BorderVisible(false)
  , Buttons(SplitVertical | SplitHorizontal | Maximize | Close)
  , UniqueID(QUuid::createUuid())
  , Internals(new pqViewFrame::pqInternals(this))
{
  Ui::pqViewFrame& ui = this->Internals->Ui;

  this->ToolBar = new QToolBar(this);
  this->ToolBar->setObjectName("ToolBar");
  this->ToolBar->setIconSize(QSize(ICON_SIZE, ICON_SIZE));
  this->ToolBar->layout()->setSpacing(0);
  this->ToolBar->layout()->setContentsMargins(0, 0, 0, 0);

  this->connect(ui.TitleBar, SIGNAL(customContextMenuRequested(const QPoint&)),
    SLOT(contextMenuRequested(const QPoint&)));

  // To handle drag/drop event.
  ui.TitleBar->installEventFilter(this);

  // Create standard buttons.
  this->StandardToolButtons[SplitVertical] = this->createButton(
    new QAction(QIcon(":/pqWidgets/Icons/pqSplitViewV12.png"), "Split Vertical", this)
    << pqSetName("SplitVertical"));
  this->StandardToolButtons[SplitHorizontal] = this->createButton(
    new QAction(QIcon(":/pqWidgets/Icons/pqSplitViewH12.png"), "Split Horizontal", this)
    << pqSetName("SplitHorizontal"));
  this->StandardToolButtons[Maximize] = this->createButton(
    new QAction(
      QIcon(this->style()->standardPixmap(QStyle::SP_TitleBarMaxButton)), "Maximize", this)
    << pqSetName("Maximize"));
  this->StandardToolButtons[Restore] = this->createButton(
    new QAction(
      QIcon(this->style()->standardPixmap(QStyle::SP_TitleBarNormalButton)), "Restore", this)
    << pqSetName("Minimize"));
  this->StandardToolButtons[Close] = this->createButton(
    new QAction(QIcon(this->style()->standardPixmap(QStyle::SP_TitleBarCloseButton)), "Close", this)
    << pqSetName("Close"));

  // Setup the title bar.
  ui.TitleBarLayout->insertWidget(0, this->ToolBar);
  foreach (QToolButton* button, this->StandardToolButtons)
  {
    ui.TitleBarLayout->addWidget(button);
  }

  this->ContextMenu = new QMenu(ui.TitleBar), this->ContextMenu->setObjectName("FrameContextMenu");
  this->ContextMenu->addAction(this->StandardToolButtons[SplitHorizontal]->defaultAction());
  this->ContextMenu->addAction(this->StandardToolButtons[SplitVertical]->defaultAction());
  this->ContextMenu->addAction(this->StandardToolButtons[Close]->defaultAction());

  this->setBorderColor(QColor("blue"));
  this->Internals->Ui.TitleLabel->installEventFilter(this);
}

//-----------------------------------------------------------------------------
pqViewFrame::~pqViewFrame()
{
}

//-----------------------------------------------------------------------------
void pqViewFrame::setBorderColor(const QColor& clr)
{
  if (this->BorderColor != clr)
  {
    this->BorderColor = clr;
    this->updateComponentVisibilities();
  }
}

//-----------------------------------------------------------------------------
void pqViewFrame::setCentralWidget(QWidget* widget, pqView* view)
{
  this->CentralWidget = widget;
  Ui::pqViewFrame& ui = this->Internals->Ui;
  while (QLayoutItem* item = ui.CentralWidgetFrameLayout->takeAt(0))
  {
    delete item;
  }
  if (this->CentralWidget)
  {
    ui.CentralWidgetFrameLayout->addWidget(this->CentralWidget);
  }
  if (view)
  {
    this->connect(view, SIGNAL(nameChanged(pqServerManagerModelItem*)),
      SLOT(onViewNameChanged(pqServerManagerModelItem*)));
  }
}

//-----------------------------------------------------------------------------
void pqViewFrame::onViewNameChanged(pqServerManagerModelItem* item)
{
  pqView* view = dynamic_cast<pqView*>(item);
  if (view)
  {
    this->setTitle(view->getSMName());
  }
}

//-----------------------------------------------------------------------------
QWidget* pqViewFrame::centralWidget() const
{
  return this->CentralWidget;
}

//-----------------------------------------------------------------------------
void pqViewFrame::setTitle(const QString& text)
{
  this->PlainTitle = text;
  this->updateComponentVisibilities();
}

//-----------------------------------------------------------------------------
QString pqViewFrame::title() const
{
  return this->Internals->Ui.TitleLabel->text();
}

//-----------------------------------------------------------------------------
void pqViewFrame::updateComponentVisibilities()
{
  bool showTitleBar = this->DecorationsVisible && this->TitleBarVisible;
  bool showBorder = this->DecorationsVisible && this->BorderVisible;

  Ui::pqViewFrame& ui = this->Internals->Ui;
  ui.TitleBar->setVisible(showTitleBar);
  if (showBorder)
  {
    ui.CentralWidgetFrame->setForegroundRole(QPalette::WindowText);
    ui.CentralWidgetFrame->setStyleSheet(
      QString("QFrame#CentralWidgetFrame { color: rgb(%1, %2, %3); }")
        .arg(this->BorderColor.red())
        .arg(this->BorderColor.green())
        .arg(this->BorderColor.blue()));
    ui.TitleLabel->setText(QString("<b><u>%1</u></b>").arg(this->PlainTitle));
  }
  else
  {
    ui.CentralWidgetFrame->setForegroundRole(QPalette::Window);
    ui.CentralWidgetFrame->setStyleSheet(QString());
    ui.TitleLabel->setText(this->PlainTitle);
  }
  if (this->DecorationsVisible)
  {
    ui.CentralWidgetFrame->setFrameStyle(QFrame::Plain | QFrame::Box);
    ui.CentralWidgetFrameLayout->setContentsMargins(1, 1, 1, 1);
  }
  else
  {
    ui.CentralWidgetFrame->setFrameStyle(QFrame::NoFrame);
    ui.CentralWidgetFrameLayout->setContentsMargins(0, 0, 0, 0);
  }
}

//-----------------------------------------------------------------------------
void pqViewFrame::setStandardButtons(StandardButtons buttons)
{
  if (this->Buttons != buttons)
  {
    this->Buttons = buttons;
    for (StandardToolButtonsMap::iterator iter = this->StandardToolButtons.begin();
         iter != this->StandardToolButtons.end(); ++iter)
    {
      iter.value()->setVisible(this->Buttons & iter.key());
    }
  }
}

//-----------------------------------------------------------------------------
QToolButton* pqViewFrame::createButton(QAction* action)
{
  QToolButton* toolButton = new QToolButton(this);
  toolButton->setDefaultAction(action);
  toolButton->setObjectName(action->objectName());
  toolButton->setIcon(action->icon());
  toolButton->setIconSize(QSize(ICON_SIZE, ICON_SIZE));

  QObject::connect(
    toolButton, SIGNAL(triggered(QAction*)), this, SLOT(buttonClicked()), Qt::QueuedConnection);
  return toolButton;
}

//-----------------------------------------------------------------------------
void pqViewFrame::buttonClicked()
{
  QToolButton* toolButton = qobject_cast<QToolButton*>(this->sender());
  if (toolButton)
  {
    StandardButton key = this->StandardToolButtons.key(toolButton, NoButton);
    if (key != NoButton)
    {
      emit this->buttonPressed(key);
    }
  }
}

//-----------------------------------------------------------------------------
void pqViewFrame::addTitleBarAction(QAction* action)
{
  this->ToolBar->addAction(action);
}

//-----------------------------------------------------------------------------
QAction* pqViewFrame::addTitleBarAction(const QString& titlestr)
{
  QAction* action = new QAction(titlestr, this);
  action->setObjectName(titlestr);
  this->addTitleBarAction(action);
  return action;
}

//-----------------------------------------------------------------------------
QAction* pqViewFrame::addTitleBarAction(const QIcon& icon, const QString& titlestr)
{
  QAction* action = new QAction(icon, titlestr, this);
  action->setObjectName(titlestr);
  this->addTitleBarAction(action);
  return action;
}

//-----------------------------------------------------------------------------
QAction* pqViewFrame::addTitleBarSeparator()
{
  QAction* separator = new QAction(this);
  separator->setSeparator(true);
  this->addTitleBarAction(separator);
  return separator;
}

//-----------------------------------------------------------------------------
void pqViewFrame::removeTitleBarActions()
{
  this->ToolBar->clear();
}

//-----------------------------------------------------------------------------
void pqViewFrame::contextMenuRequested(const QPoint& point)
{
  this->setFocus(Qt::OtherFocusReason);

  Ui::pqViewFrame& ui = this->Internals->Ui;
  this->ContextMenu->exec(ui.TitleBar->mapToGlobal(point));
}

//-----------------------------------------------------------------------------
bool pqViewFrame::eventFilter(QObject* caller, QEvent* evt)
{
  Q_UNUSED(caller);
  if (evt->type() == QEvent::MouseButtonPress)
  {
    QMouseEvent* mouseEvent = reinterpret_cast<QMouseEvent*>(evt);
    if (mouseEvent->button() == Qt::LeftButton)
    {
      this->DragStartPosition = mouseEvent->pos();
    }
  }
  else if (evt->type() == QEvent::MouseMove)
  {
    QMouseEvent* mouseEvent = reinterpret_cast<QMouseEvent*>(evt);
    if ((mouseEvent->buttons() & Qt::LeftButton) &&
      (mouseEvent->pos() - this->DragStartPosition).manhattanLength() >=
        QApplication::startDragDistance())
    {
      this->drag();
      return true;
    }
  }
  else if (evt->type() == QEvent::DragEnter)
  {
    QDragEnterEvent* de = reinterpret_cast<QDragEnterEvent*>(evt);
    this->dragEnter(de);
    return true;
  }
  else if (evt->type() == QEvent::Drop)
  {
    QDropEvent* de = reinterpret_cast<QDropEvent*>(evt);
    this->drop(de);
    return true;
  }
  return false;
}

//-----------------------------------------------------------------------------
void pqViewFrame::drag()
{
  QPixmap pixmap(":/pqWidgets/Icons/pqWindow16.png");

  QMimeData* mimeData = new QMimeData;
  mimeData->setText(QString("application/paraview3/%1").arg(getpid()));

  QPointer<QDrag> dragObj = new QDrag(this);
  dragObj->setMimeData(mimeData);
  dragObj->setHotSpot(QPoint(pixmap.width() / 2, pixmap.height() / 2));
  dragObj->setPixmap(pixmap);
  if (dragObj->exec() == Qt::MoveAction)
  {
    // Let the target know that the drag operation has concluded.
    emit this->finishDrag(this);
  }
  // It seems we are not supposed to call delete on QDrag. It gets deleted on
  // its own. Calling delete was causing segfaults on Linux in obscure call
  // stack.
  // delete dragObj.
}

//-----------------------------------------------------------------------------
void pqViewFrame::dragEnter(QDragEnterEvent* evt)
{
  QString mimeType = QString("application/paraview3/%1").arg(getpid());
  if (evt->source() != this && evt->mimeData()->hasText() && evt->mimeData()->text() == mimeType)
  {
    evt->acceptProposedAction();
  }
}

//-----------------------------------------------------------------------------
void pqViewFrame::drop(QDropEvent* evt)
{
  pqViewFrame* source = qobject_cast<pqViewFrame*>(evt->source());
  QString mimeType = QString("application/paraview3/%1").arg(getpid());
  if (source && source != this && evt->mimeData()->hasText() && evt->mimeData()->text() == mimeType)
  {
    this->connect(source, SIGNAL(finishDrag(pqViewFrame*)), SLOT(finishedDrag(pqViewFrame*)));
    evt->acceptProposedAction();
  }
}

//-----------------------------------------------------------------------------
void pqViewFrame::finishedDrag(pqViewFrame* source)
{
  Q_ASSERT(source != NULL);
  emit this->swapPositions(source->uniqueID().toString());
}

//-----------------------------------------------------------------------------
QMenu* pqViewFrame::contextMenu() const
{
  return this->ContextMenu;
}