• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
  • graphicsview
MarbleGraphicsGridLayout.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2009 Bastian Holst <bastianholst@gmx.de>
9 //
10 
11 // Self
12 #include "MarbleGraphicsGridLayout.h"
13 
14 // Marble
15 #include "MarbleDebug.h"
16 #include "ScreenGraphicsItem.h"
17 
18 // Qt
19 #include <QHash>
20 #include <QRectF>
21 #include <QSizeF>
22 #include <QVector>
23 
24 namespace Marble
25 {
26 
27 class MarbleGraphicsGridLayoutPrivate
28 {
29  public:
30  MarbleGraphicsGridLayoutPrivate( int rows, int columns )
31  : m_rows( rows ),
32  m_columns( columns ),
33  m_spacing( 0 ),
34  m_alignment( Qt::AlignLeft | Qt::AlignTop )
35  {
36  m_items = new ScreenGraphicsItem **[rows];
37  for ( int i = 0; i < rows; ++i ) {
38  m_items[i] = new ScreenGraphicsItem *[columns];
39  }
40  for ( int row = 0; row < rows; row++ ) {
41  for ( int column = 0; column < columns; column++ ) {
42  m_items[row][column] = 0;
43  }
44  }
45  }
46 
47  ~MarbleGraphicsGridLayoutPrivate()
48  {
49  for ( int i = 0; i < m_rows; ++i ) {
50  delete[] m_items[i];
51  }
52  delete[] m_items;
53  }
54 
55  // A two dimensional array of pointers to ScreenGraphicsItems
56  ScreenGraphicsItem ***m_items;
57  int m_rows;
58  int m_columns;
59  int m_spacing;
60  Qt::Alignment m_alignment;
61  QHash<ScreenGraphicsItem*, Qt::Alignment> m_itemAlignment;
62 };
63 
64 MarbleGraphicsGridLayout::MarbleGraphicsGridLayout( int rows, int columns )
65  : d( new MarbleGraphicsGridLayoutPrivate( rows, columns ) )
66 {
67 }
68 
69 MarbleGraphicsGridLayout::~MarbleGraphicsGridLayout()
70 {
71  delete d;
72 }
73 
74 void MarbleGraphicsGridLayout::addItem( ScreenGraphicsItem *item, int row, int column )
75 {
76  if ( row < d->m_rows
77  && column < d->m_columns )
78  {
79  d->m_items[row][column] = item;
80  }
81 }
82 
83 void MarbleGraphicsGridLayout::updatePositions( MarbleGraphicsItem *parent )
84 {
85  // Initialize with 0.0
86  QVector<double> maxWidth( d->m_columns, 0.0 );
87  QVector<double> maxHeight( d->m_rows, 0.0 );
88 
89  // Determining the cell sizes
90  for ( int row = 0; row < d->m_rows; row++ ) {
91  for ( int column = 0; column < d->m_columns; column++ ) {
92  if ( d->m_items[row][column] == 0 ) {
93  continue;
94  }
95 
96  QSizeF size = d->m_items[row][column]->size();
97  double width = size.width();
98  double height = size.height();
99 
100  if ( width > maxWidth[column] ) {
101  maxWidth[column] = width;
102  }
103  if ( height > maxHeight[row] ) {
104  maxHeight[row] = height;
105  }
106  }
107  }
108 
109  QVector<double> startX( d->m_columns );
110  QVector<double> endX( d->m_columns );
111  QVector<double> startY( d->m_rows );
112  QVector<double> endY( d->m_rows );
113  QRectF contentRect = parent->contentRect();
114 
115  for ( int i = 0; i < d->m_columns; i++ ) {
116  if ( i == 0 ) {
117  startX[0] = contentRect.left();
118  }
119  else if ( maxWidth[i] == 0 ) {
120  startX[i] = endX[i-1];
121  }
122  else {
123  startX[i] = endX[i-1] + d->m_spacing;
124  }
125 
126  endX[i] = startX[i] + maxWidth[i];
127  }
128 
129  for ( int i = 0; i < d->m_rows; i++ ) {
130  if ( i == 0 ) {
131  startY[0] = contentRect.left();
132  }
133  else if ( maxHeight[i] == 0 ) {
134  startY[i] = endY[i-1];
135  }
136  else {
137  startY[i] = endY[i-1] + d->m_spacing;
138  }
139 
140  endY[i] = startY[i] + maxHeight[i];
141  }
142 
143  // Setting the positions
144  for ( int row = 0; row < d->m_rows; row++ ) {
145  for ( int column = 0; column < d->m_columns; column++ ) {
146  if ( d->m_items[row][column] == 0 ) {
147  continue;
148  }
149 
150  double xPos, yPos;
151 
152  Qt::Alignment align = alignment( d->m_items[row][column] );
153 
154  if ( align & Qt::AlignRight ) {
155  xPos = endX[column] - d->m_items[row][column]->size().width();
156  }
157  else if ( align & Qt::AlignHCenter ) {
158  xPos = startX[column]
159  + ( maxWidth[column] - d->m_items[row][column]->size().width() ) / 2.0;
160  }
161  else {
162  xPos = startX[column];
163  }
164 
165  if ( align & Qt::AlignBottom ) {
166  yPos = endY[row] - d->m_items[row][column]->size().height();
167  }
168  else if ( align & Qt::AlignVCenter ) {
169  yPos = startY[row]
170  + ( maxHeight[row] - d->m_items[row][column]->size().height() ) / 2.0;
171  }
172  else {
173  yPos = startY[row];
174  }
175 
176  d->m_items[row][column]->setPosition( QPointF( xPos, yPos ) );
177  }
178  }
179 
180  parent->setContentSize( QSizeF( endX[d->m_columns - 1] - contentRect.left(),
181  endY[d->m_rows - 1] - contentRect.top() ) );
182 }
183 
184 Qt::Alignment MarbleGraphicsGridLayout::alignment() const
185 {
186  return d->m_alignment;
187 }
188 
189 Qt::Alignment MarbleGraphicsGridLayout::alignment( ScreenGraphicsItem *item ) const
190 {
191  return d->m_itemAlignment.value( item, d->m_alignment );
192 }
193 
194 void MarbleGraphicsGridLayout::setAlignment( Qt::Alignment align )
195 {
196  d->m_alignment = align;
197 }
198 
199 void MarbleGraphicsGridLayout::setAlignment( ScreenGraphicsItem *item, Qt::Alignment align )
200 {
201  d->m_itemAlignment.insert( item, align );
202 }
203 
204 int MarbleGraphicsGridLayout::spacing() const
205 {
206  return d->m_spacing;
207 }
208 
209 void MarbleGraphicsGridLayout::setSpacing( int spacing )
210 {
211  d->m_spacing = spacing;
212 }
213 
214 } // namespace Marble
QRectF::top
qreal top() const
Marble::MarbleGraphicsGridLayout::setAlignment
void setAlignment(Qt::Alignment align)
Definition: MarbleGraphicsGridLayout.cpp:194
Marble::MarbleGraphicsGridLayout::addItem
void addItem(ScreenGraphicsItem *item, int row, int column)
Definition: MarbleGraphicsGridLayout.cpp:74
Qt::Alignment
typedef Alignment
MarbleDebug.h
QRectF::left
qreal left() const
Marble::MarbleGraphicsItem::setContentSize
virtual void setContentSize(const QSizeF &size)
Set the size of the content of the item.
Definition: MarbleGraphicsItem.cpp:210
QPointF
Marble::MarbleGraphicsGridLayout::setSpacing
void setSpacing(int spacing)
Set the spacing between the items inside the layout.
Definition: MarbleGraphicsGridLayout.cpp:209
QHash
Marble::MarbleGraphicsItem
Definition: MarbleGraphicsItem.h:34
Marble::MarbleGraphicsGridLayout::alignment
Qt::Alignment alignment() const
Definition: MarbleGraphicsGridLayout.cpp:184
Marble::MarbleGraphicsGridLayout::updatePositions
void updatePositions(MarbleGraphicsItem *parent)
This updates the positions of all items in the layout.
Definition: MarbleGraphicsGridLayout.cpp:83
ScreenGraphicsItem.h
MarbleGraphicsGridLayout.h
QVector
QSizeF
Marble::MarbleGraphicsGridLayout::spacing
int spacing() const
Returns the spacing between the items inside the layout.
Definition: MarbleGraphicsGridLayout.cpp:204
QRectF
Marble::MarbleGraphicsGridLayout::MarbleGraphicsGridLayout
MarbleGraphicsGridLayout(int rows, int columns)
Definition: MarbleGraphicsGridLayout.cpp:64
Marble::ScreenGraphicsItem
Definition: ScreenGraphicsItem.h:30
Marble::MarbleGraphicsItem::contentRect
virtual QRectF contentRect() const
Returns the rect of the content in item coordinates.
Definition: MarbleGraphicsItem.cpp:215
QSizeF::height
qreal height() const
QSizeF::width
qreal width() const
Marble::MarbleGraphicsGridLayout::~MarbleGraphicsGridLayout
~MarbleGraphicsGridLayout()
Definition: MarbleGraphicsGridLayout.cpp:69
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal