FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
item_model_utils.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (30 Mar. 2015)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #pragma once
17 
18 class QAbstractItemModel;
19 
20 #include "core.h"
21 #include <QtCore/QModelIndex>
22 #include <QtCore/QVariant>
23 #include <QtCore/QVector>
24 
25 namespace qtcore {
26 
28 {
29 public:
30  static bool isValidRow(
31  const QAbstractItemModel* model,
32  int row,
33  const QModelIndex& parent = QModelIndex());
34 
35  static bool isValidColumn(
36  const QAbstractItemModel* model,
37  int col,
38  const QModelIndex& parent = QModelIndex());
39 
40  template<typename INT_CONTAINER>
41  static bool validRows(
42  const QAbstractItemModel* model,
43  const INT_CONTAINER& rows,
44  const QModelIndex& parent = QModelIndex());
45 
46  static int findDataInRow(
47  const QAbstractItemModel* model, int col, const QVariant& value);
48 
49  static QVariant tableData(
50  const QAbstractItemModel* model,
51  int row,
52  int col,
53  int role = Qt::DisplayRole);
54 
55  template<typename INT_CONTAINER>
56  static void removeRows(
57  QAbstractItemModel* model, const INT_CONTAINER& rows);
58 };
59 
60 } // namespace qtcore
61 
62 // --
63 // -- Implementation
64 // --
65 
66 #include <algorithm>
67 #include <functional>
68 #include <QtCore/QAbstractItemModel>
69 
70 namespace qtcore {
71 
72 template<typename INT_CONTAINER>
74  const QAbstractItemModel* model,
75  const INT_CONTAINER& rows,
76  const QModelIndex& parent)
77 {
78  for (auto iRow = rows.begin(); iRow != rows.end(); ++iRow) {
79  if (!ItemModelUtils::isValidRow(model, *iRow, parent))
80  return false;
81  }
82  return !rows.isEmpty();
83 }
84 
85 template<typename INT_CONTAINER>
87  QAbstractItemModel* model, const INT_CONTAINER& rows)
88 {
89  if (model == NULL)
90  return;
91  // Delete rows by descending order
92  QVector<int> descRows;
93  for (auto it = rows.begin(); it != rows.end(); ++it) {
94  if (ItemModelUtils::isValidRow(model, *it))
95  descRows.append(*it);
96  }
97  std::sort(descRows.begin(), descRows.end(), std::greater<int>());
98  foreach (int row, descRows)
99  model->removeRow(row);
100 }
101 
102 } // namespace qtcore
static void removeRows(QAbstractItemModel *model, const INT_CONTAINER &rows)
Definition: item_model_utils.h:86
static bool isValidRow(const QAbstractItemModel *model, int row, const QModelIndex &parent=QModelIndex())
Definition: item_model_utils.cpp:27
#define QTTOOLS_CORE_EXPORT
Definition: core.h:27
Definition: grid_numbering.cpp:19
Provides a collection of tools around QAbstractItemModel.
Definition: item_model_utils.h:27
static bool validRows(const QAbstractItemModel *model, const INT_CONTAINER &rows, const QModelIndex &parent=QModelIndex())
Definition: item_model_utils.h:73