File: kwin4view.cpp

package info (click to toggle)
kfourinline 4:18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,784 kB
  • sloc: cpp: 7,339; sh: 8; makefile: 5
file content (546 lines) | stat: -rw-r--r-- 16,349 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
/*
   This file is part of the KDE games kwin4 program
   Copyright (c) 2006 Martin Heni <kde@heni-online.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// Header includes
#include "kwin4view.h"

// Standard includes
#include <cmath>

// Qt includes
#include <QColor>
#include <QEvent>
#include <QTime>

// KDE includes
#include "kfourinline_debug.h"

#define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
#include <libkdegamesprivate/kgame/kplayer.h>

// Local includes
#include "kwin4global.h"
#include "displayintro.h"
#include "displaygame.h"
#include "spritenotify.h"
#include "score.h"
#include "reflectiongraphicsscene.h"

// How many time measurements for average
#define MEASUREMENT_LIST_SIZE  50
// How many warnings until reflections are switched off
#define WARNING_MAX_COUNT      5
// How many milliseconds rounding error
#define MEASUREMENT_ROUNDING_ERROR 5


// Constructor for the view
KWin4View::KWin4View(int updateTime, 
                     const QSize &size, 
                     ReflectionGraphicsScene* scene,
                     ThemeManager* theme, 
                     QWidget* parent)
          : Themeable(QStringLiteral("theview"), theme), QGraphicsView(scene, parent)
{
  // Store attributes    
  mScene             = scene;
  mTheme             = theme;
  mDefaultUpdateTime = updateTime;
  mSlowDownFactor    = 1.0;
  mSlowCnt           = 0;
  mReflectPhase      = 0; 

  // We do not need scrolling so switch it off
  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  setFrameStyle(QFrame::NoFrame);
  setCacheMode(QGraphicsView::CacheBackground);
  //setAlignment(Qt::AlignHCenter);

  //setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
  setOptimizationFlags(QGraphicsView::DontClipPainter |
                       QGraphicsView::DontSavePainterState |
                       QGraphicsView::DontAdjustForAntialiasing );

  viewport()->setMouseTracking(true);
  setMouseTracking(true);

  // Choose a background color
  scene->setBackgroundBrush(QColor(0,0,128));


  mTimer = new QTimer(this);
  connect(mTimer, &QTimer::timeout, this, &KWin4View::updateAndAdvance);
  mTimer->start(mDefaultUpdateTime);

  // Game status
  mIsRunning = false;

  // Queue
  mThemeQueue.clear();
  mThemeOffset.clear();
  
  // Set size and position of the view and the canvas:
  // they are reseized once a level is loaded
  resize(size);
  scene->setSceneRect(0, 0, this->width(), this->height()); 
  adjustSize();

  // Interact with user
  setInteractive(true);
  
  // Scale theme
  mTheme->rescale(this->width(), QPoint(0,0));

  // Start with the intro display
  mGameDisplay  = 0;
  mIntroDisplay  = 0;


  // Reflections
  mReflectionSprite = new QGraphicsPixmapItem(0);
  scene->addItem(mReflectionSprite);
  mReflectionSprite->setZValue(1000.0);
  mReflectionSprite->hide();

  // Debug                      
  mFrameSprite = new QGraphicsTextItem(0);
  scene->addItem(mFrameSprite);
  mFrameSprite->setPos(QPointF(0.0, 0.0));
  mFrameSprite->setZValue(1000.0);
  if (global_debug > 0) mFrameSprite->show();
  else mFrameSprite->hide();

  
  // Skip the intro?
  if (!global_skip_intro)
  {
    mIntroDisplay = new DisplayIntro(scene, mTheme, this);
    connect(mIntroDisplay, &DisplayIntro::signalQuickStart, this, &KWin4View::signalQuickStart);
    mIntroDisplay->start();
  }
}


// Destruct the view object
KWin4View::~KWin4View()
{
  delete mIntroDisplay;
  delete mGameDisplay;
  if (global_debug>0) qCDebug(KFOURINLINE_LOG) << "TRACKING" << hasMouseTracking() << "and" << viewport()->hasMouseTracking();
  delete mFrameSprite;
  delete mReflectionSprite;
}

// Main themeable function. Called for any theme change. 
void KWin4View::changeTheme()
{
  if (global_debug > 0) qCDebug(KFOURINLINE_LOG) << "CHANGE THEME IN VIEW ... resetting slow counter";
  mDrawTimes.clear();
  mSlowDownFactor = 1.0;
  mSlowCnt = 0;
  mTimer->setInterval(int(mDefaultUpdateTime*mSlowDownFactor)); 
}


// Advance and update canvas/scene
void KWin4View::updateAndAdvance()
{
  // Time measurement (maybe remove static at some point)  
  static bool first = true;
  static QTime time;
  int elapsed = time.elapsed();
  if (first) {elapsed = 0;first=false;}
  time.restart();

  // Time display
  mDrawTimes.append(elapsed);
  if (mDrawTimes.size() > MEASUREMENT_LIST_SIZE) mDrawTimes.removeFirst();
  double avg = 0.0;
  for (int i=0; i<mDrawTimes.size(); i++) avg += mDrawTimes[i];
  avg /= mDrawTimes.size();

  // Set debug sprite
  if (global_debug > 0)
  {
     mFrameSprite->setPlainText(QStringLiteral("CurrentUpdate: %1 ms  AverageUpdate%2 ms  DefaultUpdate: %3*%4 ms").
                  arg(elapsed).arg(int(avg)).arg(mDefaultUpdateTime).arg(mSlowDownFactor));
  }
     
  
  // Dynamic update of the graphics advance and update speed
  if (mDrawTimes.size() == MEASUREMENT_LIST_SIZE && 
      avg > mDefaultUpdateTime*mSlowDownFactor+MEASUREMENT_ROUNDING_ERROR)
  {
    mSlowCnt++;
    qCDebug(KFOURINLINE_LOG) << "Warning " << mSlowCnt << " avg=" << avg;
    mDrawTimes.clear();
    if (mSlowCnt > WARNING_MAX_COUNT)
    {
      mSlowDownFactor = double(MEASUREMENT_ROUNDING_ERROR+avg)/double(mDefaultUpdateTime);
      mSlowCnt = 0;
      mTimer->setInterval(int(mDefaultUpdateTime*mSlowDownFactor)); 

      qCDebug(KFOURINLINE_LOG) << "SLOW COMPUTER WARNING: Decreasing graphics update speed "
               << mDefaultUpdateTime*mSlowDownFactor<<"ms. Maybe switch off reflections.";
    }
  }

    
  // Scene advance  
  scene()->advance();
  // QGV takes care of updating dirty rects, no need to call update or the whole scene is dirtied and repainted
  // scene()->update();


  // ====================================================================================
  // Reflections need to be done in the view otherwise the update's go wrong
  if (mReflectionRect.width() >0 && mReflectionRect.height() > 0)
  {
    // Draw reflection in steps to save processing power
    if (mReflectPhase == 0)
    {
      mReflectImage = QImage(mReflectionRect.width(), mReflectionRect.height(), QImage::Format_ARGB32);
      mReflectImage.fill(Qt::transparent);
      QPainter imagePainter(&mReflectImage);
      // imagePainter.fillRect(image.rect(),QBrush(Qt::red));

      //Turn on all optimizations
      imagePainter.setRenderHints(QPainter::Antialiasing |
                                  QPainter::TextAntialiasing |
                                  QPainter::SmoothPixmapTransform, false);
      imagePainter.setClipping(true);
      imagePainter.setWorldMatrix(QMatrix(1.0,0.0,0.0,-1.0,0.0,mReflectImage.height()));
      QRect source = QRect(mReflectionRect.x(),mReflectionRect.y()-mReflectImage.height(), 
                           mReflectImage.width(), mReflectImage.height());

      bool vis = mReflectionSprite->isVisible();
      mReflectionSprite->hide();
      dynamic_cast<ReflectionGraphicsScene*>(scene())->setBackground(false);
      scene()->render(&imagePainter, mReflectImage.rect(), source, Qt::IgnoreAspectRatio);
      dynamic_cast<ReflectionGraphicsScene*>(scene())->setBackground(true);
      if (vis) mReflectionSprite->show();
      mReflectPhase = 1;
    }
    // Draw reflection in steps to save processing power
    else if (mReflectPhase == 1)
    {
       // Semi transparent
      QPainter imagePainter(&mReflectImage);
      imagePainter.setTransform(QTransform());
      imagePainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
      imagePainter.drawImage(0,0,mGradientImage);
      mReflectPhase = 2;
    }
    // Draw reflection in steps to save processing power
    else if (mReflectPhase == 2)
    {
       // Set to sprite
      QPixmap pm = QPixmap::fromImage(mReflectImage);
      mReflectionSprite->setPixmap(pm);
      mReflectionSprite->update();
      mReflectPhase = 0;
    }
  }
  // ====================================================================================

}


// Define the reflection
void KWin4View::setReflection(int x, int y, int width, int height)
{
  mReflectionRect = QRect(x,y,width,height);

  QPoint p1, p2;
  p2.setY(height);
  mGradient = QLinearGradient(p1, p2);
  mGradient.setColorAt(0, QColor(0, 0, 0, 100));
  mGradient.setColorAt(1, Qt::transparent);

  qCDebug(KFOURINLINE_LOG) << "Set reflection "<< x << " " << y << " " << width << " " << height ;

  mGradientImage = QImage(width, height, QImage::Format_ARGB32);
  mGradientImage.fill(Qt::transparent);
  QPainter p( &mGradientImage );
  p.fillRect(0,0,width, height, mGradient);
  p.end();

  mReflectionSprite->setPos(x,y);
  if (width >0 && height > 0)
  {
    mReflectionSprite->show();
  }
  else
  {
    mReflectionSprite->hide();
  }
}


// QGV drawItems function (for debug time measurements)
void KWin4View::drawItems(QPainter* painter, int numItems, QGraphicsItem* items[], const QStyleOptionGraphicsItem options[])
{
  QGraphicsView::drawItems(painter, numItems, items, options);
}


// Stop intro display and init game display
void KWin4View::initGame(Score* scoreData)
{
  qCDebug(KFOURINLINE_LOG) << "KWin4View::initGame";

  // For better performance disable mouse tracking now
  viewport()->setMouseTracking(false);
  setMouseTracking(false);

  delete mIntroDisplay;
  mIntroDisplay = 0;
  if (!mGameDisplay)
  {
     mGameDisplay = new DisplayGame(mScene, mTheme, this);
  }
  mGameDisplay->start();

  // Connect score and score sprite
  scoreData->setDisplay(mGameDisplay->score());

  mIsRunning = true;
}


// End the game
void  KWin4View::endGame()
{
  mIsRunning = false;
  mGameDisplay->displayEnd();
}


// Slot called by the framework when the view is resized.
void KWin4View::resizeEvent (QResizeEvent* e)
{
 if (global_debug > 2) qCDebug(KFOURINLINE_LOG) <<"RESIZE EVENT" << e->size() << "oldSize="<< e->oldSize();

  // Test to prevent double resizing
  // if (QWidget::testAttribute(Qt::WA_PendingResizeEvent))
  // {
  //   return;
  // }

  double diffW = double(e->oldSize().width()-e->size().width());
  double diffH = double(e->oldSize().height()-e->size().height());
  double delta = fabs(diffW) + fabs(diffH); 



  // Adapt the canvas size to the window size
  if (scene())
  {
    scene()->setSceneRect(0, 0, e->size().width(), e->size().height());
  }
  QSizeF size = QSizeF(e->size());

  // Rescale on minimum fitting aspect ratio either width or height limiting
  double width = 0.0;
  double aspect = size.width() / size.height();
  QPoint offset;

  // Scale width:
  // Ideal size would be: 'width'*'height'
  // Offset in width is (e->size().width()-width)/2, offset in height is zero
  if (aspect > mTheme->aspectRatio())
  {
     width = e->size().height()*mTheme->aspectRatio(); 
     offset = QPoint(int((e->size().width()-width)/2.0), 0);
  }
  // Scale height:
  // 'height' = width/mTheme->aspectRatio()
  // Ideal size would be: 'width'*'height': 
  // Offset in height is (e->size().height()-width/mTheme->aspectRatio())/2, offset in width is zero
  else
  {
     width = e->size().width();  // Scale height
     offset = QPoint(0, int((e->size().height()-width/mTheme->aspectRatio())/2.0));
  }
  
  
  // Pixel rescale
  double oldScale = mTheme->getScale();

  //resetTransform();
  QTransform transform;
  if (width > oldScale) 
  {
    transform.scale(double(width/oldScale), double(width/oldScale));
  }
  setTransform(transform);
  
  mThemeQueue.prepend(int(width));
  mThemeOffset.prepend(offset);
  if (global_debug > 2) qCDebug(KFOURINLINE_LOG) << "Quequed resize, aspect=" << aspect << "theme aspect="<< mTheme->aspectRatio();

  long queueDelay = 0;
  if (delta < 15) queueDelay = 750;
  else if (delta < 35) queueDelay = 500;

  QTimer::singleShot(queueDelay, this, &KWin4View::rescaleTheme );
}


// Rescale the theme (update theme SVG graphics) from the theme list
void KWin4View::rescaleTheme()
{
  if (mThemeQueue.size() == 0)
  {
    if (global_debug > 2) qCDebug(KFOURINLINE_LOG) << "***************** Swallowing rescale event ***********************";
    return;
  }

  QTime t;
  t.start();

  resetTransform();
  
  int width     = mThemeQueue.first();
  QPoint offset = mThemeOffset.first();
  if (global_debug > 2) qCDebug(KFOURINLINE_LOG) << "Theme queue size=" << mThemeQueue.size() << "Rescale width to" << width 
                                 << " offset " << offset;
  mThemeQueue.clear();
  mThemeOffset.clear();
  mTheme->rescale(width, offset);

   if (global_debug > 2) qCDebug(KFOURINLINE_LOG) << "Time elapsed: "<< t.elapsed() << "ms";
}




// This slot is called when a mouse key is pressed. As the mouse is used as
// input for all players. It is called to generate a player move out of a mouse input, i.e.
// it converts a QMouseEvent into a move for the game. 
void KWin4View::mouseInput(KGameIO* input, QDataStream& stream, QMouseEvent* mouse, bool* eatevent)
{
  // Only react to mouse pressed not released
  if (mouse->type()   != QEvent::MouseButtonPress ) return;
  if (mouse->button() != Qt::LeftButton) return ;
  if (!mIsRunning) return;

  // Our player
  KPlayer* player=input->player();
  if (!player->myTurn())
  {
    // qCDebug(KFOURINLINE_LOG) <<" Kwin4View::TODO wrongPlayer";
  //  *eatevent=wrongPlayer(player,KGameIO::MouseIO);
    return;
  }

  // Calculate movement position from mouse position
  int x = -1;
  if (mGameDisplay) x = mGameDisplay->mapMouseToMove(mouse->pos());
  if (x<0) return;

  // Create a game move (pl id and move coordinate)
  qint32 move = x;
  qint32 pl   = player->userId();
  stream << pl << move;
  *eatevent=true;
}


// This slot is called when a key event is received. It then produces a
// valid move for the game.
// This is analogous to the mouse event only it is called when a key is
// pressed.
void KWin4View::keyInput(KGameIO* input, QDataStream& stream, QKeyEvent* key, bool* eatevent)
{
  // Ignore non running
  if (!mIsRunning) return;

  // Ignore non key press
  if (key->type() != QEvent::KeyPress) return ;

  // Check key code
  int code=key->key();
  if (code< Qt::Key_1 || code> Qt::Key_7) return ;
  

  // Our player
  KPlayer *player=input->player();
  if (!player->myTurn())
  {
    //qCDebug(KFOURINLINE_LOG) <<" Kwin4View::TODO wrongPlayer";
   // *eatevent=wrongPlayer(player,KGameIO::KeyIO);
    return;
  }

  // Create a valid game move (player id and movement position)
  qint32 move = code-Qt::Key_1;
  qint32 pl   = player->userId();
  stream << pl << move;
  *eatevent=true;
}



// Displays a move on the game board. 
void KWin4View::displayMove(int x, int y, int color, int xarrow, int colorarrow, int no, bool animation)
{
  mGameDisplay->displayArrow(xarrow, colorarrow);
  // animation only if no redo
  SpriteNotify* notify = mGameDisplay->displayPiece(x, y, color, no, animation);
  if (notify && animation)
  {
    QObject::disconnect(notify,&SpriteNotify::signalNotify,
                        this,&KWin4View::moveDone);
    connect(notify, &SpriteNotify::signalNotify, this, &KWin4View::moveDone);
  }
  mGameDisplay->displayHint(0,0,false);
}


// Display a star of the given sprite number
void KWin4View::displayStar(int x, int y, int no)
{
  mGameDisplay->displayStar(x, y, no);
}

// Display a hint on the board
void KWin4View::displayHint(int x, int y)
{
  mGameDisplay->displayHint(x, y, true);
}

// Slot called when a sprite animation move is done.
void KWin4View::moveDone(QGraphicsItem* /*item*/, int mode)
{
  emit signalMoveDone(mode);
}


bool KWin4View::viewportEvent ( QEvent * event ) 
{
  if (mIntroDisplay) mIntroDisplay->viewEvent(event);
  return QGraphicsView::viewportEvent(event);
}