File: partview.cpp

package info (click to toggle)
cuba 3.0%2B20111124-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 984 kB
  • ctags: 677
  • sloc: ansic: 6,090; sh: 343; fortran: 216; makefile: 216; cpp: 189
file content (261 lines) | stat: -rw-r--r-- 5,874 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
/*
	partview.cpp
		Partition viewer for Cuba
		last modified 15 Feb 11 th
*/


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <QWidget>
#include <QMainWindow>
#include <QTabWidget>
#include <QAction>
#include <QToolBar>
#include <QPainter>
#include <QPrinter>
#include <QPrintDialog>
#include <QApplication>
#include <QColorGroup>

#include <list>

#include "quit.xpm"
#include "print.xpm"

// --------------------------------------------------------------

class PartitionPlane : public QWidget
{
public:
  PartitionPlane( const int dimx, const int dimy, QWidget *parent = 0 )
  : QWidget(parent), m_dimx(dimx), m_dimy(dimy), m_got(0) {
  }

  void addBound( const int dim, const double lower, const double upper );

  void drawRegion( QPainter *p, const QRect &r );
  void drawRegions( QPainter *p );

  QSize sizeHint() const {
    return QSize(InitialSize, InitialSize);
  }

  QString filename() const {
    return QString("%1-%2.ps").arg(m_dimx).arg(m_dimy);
  }

protected:
  void paintEvent( QPaintEvent * ) {
    QPainter p(this);
    drawRegions( &p );
  }

private:
  int m_dimx, m_dimy, m_got;
  int m_xlower, m_xupper, m_ylower, m_yupper;

  typedef std::list<QRect> regionList;
  typedef regionList::iterator regionIt;
  regionList m_regions;

  enum {
    Hue = 0, // red
    InitialSize = 400,
    CoordScale = 4*InitialSize };
};


void PartitionPlane::addBound( const int dim,
  const double lower, const double upper )
{
  if( dim == m_dimx ) {
    m_xlower = int(CoordScale*lower);
    m_xupper = int(CoordScale*upper);
    m_got |= 1;
  }
  if( dim == m_dimy ) {
    m_ylower = int(CoordScale*lower);
    m_yupper = int(CoordScale*upper);
    m_got |= 2;
  }

  if( m_got == 3 ) {
    m_got = 0;

    const QRect rect = QRect(m_xlower, CoordScale - m_yupper,
      m_xupper - m_xlower + 1, m_yupper - m_ylower + 1);

    const int area = rect.width()*rect.height();
    regionIt r;

    for( r = m_regions.begin(); r != m_regions.end(); ++r ) {
      if( rect == *r ) return;
      if( area > (*r).width()*(*r).height() ) break;
    }
    m_regions.insert(r, rect);

    QPainter p(this);
    drawRegion( &p, rect );
  }
}


void PartitionPlane::drawRegion( QPainter *p, const QRect &r )
{
  p->setWindow(0, 0, CoordScale, CoordScale);

  QColor c;
  const double ratio = r.width()*r.height()/
    double(CoordScale*CoordScale);
  const int saturation = int(255/(M_PI/2)*asin(1 - ratio));
  c.setHsv( Hue, saturation, 255 );
  p->setBrush(c);

  p->setPen(palette().foreground().color());

  p->drawRect(r);
}


void PartitionPlane::drawRegions( QPainter *p )
{
  for( regionIt r = m_regions.begin(); r != m_regions.end(); ++r )
    drawRegion( p, *r );
}


// --------------------------------------------------------------

class PartitionViewer : public QMainWindow
{
  Q_OBJECT

public:
  PartitionViewer( QWidget *parent );
  void addPlane( const int dimx, const int dimy );
  void addBound( const int dim, const double lower, const double upper );
  int count() const { return m_tabs->count(); }
  void tabupdate() { 
    if( m_tabs->currentWidget() != 0 ) m_tabs->currentWidget()->update();
  }

public slots:
  void print();

private:
  QTabWidget *m_tabs;
  QPrinter *m_printer;
};


PartitionViewer::PartitionViewer( QWidget *parent = 0 )
: QMainWindow(parent)
{
  setWindowTitle(tr("Cuba Partition Viewer"));

  QToolBar *toolbar = new QToolBar(this);
  addToolBar(Qt::LeftToolBarArea, toolbar);

  QAction *quit = new QAction( QPixmap(quit_xpm), tr("&Quit"), this );
  connect( quit, SIGNAL(activated()), qApp, SLOT(quit()) );
  toolbar->addAction(quit);

#ifndef QT_NO_PRINTER
  QAction *print = new QAction( QPixmap(print_xpm), tr("&Print..."), this );
  connect( print, SIGNAL(activated()), this, SLOT(print()) );
  toolbar->addAction(print);

  m_printer = new QPrinter;
#endif

  m_tabs = new QTabWidget(this);
  setCentralWidget(m_tabs);
}


void PartitionViewer::addPlane( const int dimx, const int dimy )
{
  PartitionPlane *plane = new PartitionPlane(dimx, dimy, this);
  m_tabs->addTab( plane, tr("%1-%2 plane").arg(dimx).arg(dimy) );
}


void PartitionViewer::addBound( const int dim,
  const double lower, const double upper )
{
  for( int index = 0; index < m_tabs->count(); ++index ) {
    PartitionPlane *plane = (PartitionPlane *)m_tabs->widget(index);
    if( plane ) plane->addBound(dim, lower, upper);
  }
}


void PartitionViewer::print()
{
#ifndef QT_NO_PRINTER
  PartitionPlane *plane = (PartitionPlane *)m_tabs->currentWidget();
  if( !plane ) return;

  QPrintDialog printDialog(m_printer, this);
  if( printDialog.exec() == QDialog::Accepted ) {
    QPainter p;
    if( p.begin(m_printer) ) {
      p.setViewport( QRect(QPoint(0, 0), plane->sizeHint()) );
      plane->drawRegions( &p );
    }
  }
#endif
}


#include "partview.moc"

// --------------------------------------------------------------

int main( int argc, char **argv )
{
  QApplication app(argc, argv);
  PartitionViewer partview;

  argc = (argc - 1) & -2;

  for( int arg = 0; arg < argc; ) {
    const int dimx = atoi(argv[++arg]);
    const int dimy = atoi(argv[++arg]);
    if( dimx > 0 && dimy > 0 ) partview.addPlane(dimx, dimy);
  }

  if( partview.count() == 0 ) {
    fprintf(stderr, "Usage:  %s dimx dimy ...\n"
      "reads Cuba's verbose = 3 output from stdin and displays\n"
      "the dimx-dimy plane of the tessellation on screen.\n"
      "Each pair of dimensions is shown in a separate window.\n\n",
      argv[0]);
    exit(1);
  }

  partview.show();

  int dim = 0;
  char line[128];

  while( fgets(line, sizeof line, stdin) ) {
    double lower, upper;

    fputs(line, stdout);

    if( sscanf(line, "%*[^(](%lf) - (%lf)", &lower, &upper) == 2 )
      partview.addBound(++dim, lower, upper);
    else dim = 0;

    app.processEvents();
  }

  fflush(stdout);
  partview.tabupdate();
  return app.exec();
}