~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/widgets/stretchheaderview.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
*/
17
17
 
18
18
#include "stretchheaderview.h"
 
19
#include "core/logging.h"
19
20
 
20
21
#include <QDataStream>
21
 
#include <QtDebug>
22
22
 
23
23
#include <algorithm>
24
24
#include <cmath>
25
25
#include <numeric>
26
26
 
27
27
const int StretchHeaderView::kMinimumColumnWidth = 10;
 
28
const int StretchHeaderView::kMagicNumber = 0x502c950f;
28
29
 
29
30
StretchHeaderView::StretchHeaderView(Qt::Orientation orientation, QWidget* parent)
30
31
  : QHeaderView(orientation, parent),
47
48
  if (!stretch_enabled_)
48
49
    return;
49
50
 
50
 
  float total_sum = std::accumulate(column_widths_.begin(), column_widths_.end(), 0.0);
51
 
  float selected_sum = total_sum;
 
51
  const ColumnWidthType total_sum =
 
52
      std::accumulate(column_widths_.begin(), column_widths_.end(), 0.0);
 
53
  ColumnWidthType selected_sum = total_sum;
52
54
 
53
55
  if (!sections.isEmpty()) {
54
56
    selected_sum = 0.0;
57
59
        selected_sum += column_widths_[i];
58
60
  }
59
61
 
60
 
  if (total_sum != 0.0 && !qFuzzyCompare(total_sum, 1.0f)) {
61
 
    const float mult = (selected_sum + (1.0 - total_sum)) / selected_sum;
 
62
  if (total_sum != 0.0 && !qFuzzyCompare(total_sum, 1.0)) {
 
63
    const ColumnWidthType mult = (selected_sum + (1.0 - total_sum)) / selected_sum;
62
64
    for (int i=0 ; i<column_widths_.count() ; ++i) {
63
65
      if (sections.isEmpty() || sections.contains(i))
64
66
        column_widths_[i] *= mult;
70
72
  if (!stretch_enabled_)
71
73
    return;
72
74
 
73
 
  float total_w = 0.0;
 
75
  ColumnWidthType total_w = 0.0;
74
76
 
75
77
  for (int i=0 ; i<column_widths_.count() ; ++i) {
76
 
    const float w = column_widths_[i];
 
78
    const ColumnWidthType w = column_widths_[i];
77
79
    int pixels = w * width();
78
80
 
79
81
    if (pixels != 0 && total_w - int(total_w) > 0.5)
168
170
 
169
171
  if (in_mouse_move_event_) {
170
172
    // Update this section's proportional width
171
 
    column_widths_[logical] = float(new_size) / width();
 
173
    column_widths_[logical] = ColumnWidthType(new_size) / width();
172
174
 
173
175
    // Find the visible sections to the right of the section that's being resized
174
176
    int visual = visualIndex(logical);
199
201
    // Initialise the list of widths from the current state of the widget
200
202
    column_widths_.resize(count());
201
203
    for (int i=0 ; i<count() ; ++i) {
202
 
      column_widths_[i] = float(sectionSize(i)) / width();
 
204
      column_widths_[i] = ColumnWidthType(sectionSize(i)) / width();
203
205
    }
204
206
 
205
207
    // Stretch the columns to fill the widget
210
212
  emit StretchEnabledChanged(enabled);
211
213
}
212
214
 
213
 
void StretchHeaderView::SetColumnWidth(int logical, float width) {
 
215
void StretchHeaderView::SetColumnWidth(int logical, ColumnWidthType width) {
214
216
  if (!stretch_enabled_)
215
217
    return;
216
218
 
229
231
    resizeSection(logical, kMinimumColumnWidth);
230
232
  }
231
233
}
 
234
 
 
235
bool StretchHeaderView::RestoreState(const QByteArray& data) {
 
236
  QDataStream s(data);
 
237
  s.setVersion(QDataStream::Qt_4_6);
 
238
 
 
239
  int magic_number = 0;
 
240
  s >> magic_number;
 
241
 
 
242
  if (magic_number != kMagicNumber || s.atEnd()) {
 
243
    return false;
 
244
  }
 
245
 
 
246
  QList<int> pixel_widths;
 
247
  QList<int> visual_indices;
 
248
  int sort_indicator_order = Qt::AscendingOrder;
 
249
  int sort_indicator_section = 0;
 
250
 
 
251
  s >> stretch_enabled_;
 
252
  s >> pixel_widths;
 
253
  s >> visual_indices;
 
254
  s >> column_widths_;
 
255
  s >> sort_indicator_order;
 
256
  s >> sort_indicator_section;
 
257
 
 
258
  setSortIndicator(sort_indicator_section, Qt::SortOrder(sort_indicator_order));
 
259
 
 
260
  const int persisted_column_count =
 
261
      qMin(qMin(visual_indices.count(), pixel_widths.count()), column_widths_.count());
 
262
 
 
263
  // Set column visible state, visual indices and, if we're not in stretch mode,
 
264
  // pixel widths.
 
265
  for (int i=0 ; i<count() && i<persisted_column_count ; ++i) {
 
266
    setSectionHidden(i, pixel_widths[i] <= kMinimumColumnWidth);
 
267
    moveSection(visualIndex(visual_indices[i]), i);
 
268
 
 
269
    if (!stretch_enabled_) {
 
270
      resizeSection(i, pixel_widths[i]);
 
271
    }
 
272
  }
 
273
 
 
274
  if (stretch_enabled_) {
 
275
    // In stretch mode, we've already set the proportional column widths so apply
 
276
    // them now.
 
277
    UpdateWidths();
 
278
  }
 
279
 
 
280
  emit StretchEnabledChanged(stretch_enabled_);
 
281
 
 
282
  return true;
 
283
}
 
284
 
 
285
QByteArray StretchHeaderView::SaveState() const {
 
286
  QByteArray ret;
 
287
  QDataStream s(&ret, QIODevice::WriteOnly);
 
288
 
 
289
  QList<int> pixel_widths;
 
290
  QList<int> visual_indices;
 
291
 
 
292
  for (int i=0 ; i<count() ; ++i) {
 
293
    pixel_widths << sectionSize(i);
 
294
    visual_indices << logicalIndex(i);
 
295
  }
 
296
 
 
297
  s.setVersion(QDataStream::Qt_4_6);
 
298
  s << kMagicNumber;
 
299
 
 
300
  s << stretch_enabled_;
 
301
  s << pixel_widths;
 
302
  s << visual_indices;
 
303
  s << column_widths_;
 
304
  s << int(sortIndicatorOrder());
 
305
  s << sortIndicatorSection();
 
306
 
 
307
  return ret;
 
308
}