Advertisement
Guest User

Untitled

a guest
Jan 19th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1.  
  2. #include <QtGui>
  3.  
  4. #include "model.h"
  5. #include <QDebug>
  6.  
  7. /*!
  8.     Constructs a table model with at least one row and one column.
  9. */
  10.  
  11. TableModel::TableModel(int rows, int columns, QObject *parent)
  12.     : QAbstractTableModel(parent)
  13. {
  14.     QStringList newList;
  15.  
  16.     for (int column = 0; column < qMax(1, columns); ++column) {
  17.         newList.append("");
  18.     }
  19.  
  20.     for (int row = 0; row < qMax(1, rows); ++row) {
  21.         rowList.append(newList);
  22.     }
  23. }
  24.  
  25.  
  26. /*!
  27.     Returns the number of items in the row list as the number of rows
  28.     in the model.
  29. */
  30.  
  31. int TableModel::rowCount(const QModelIndex &/*parent*/) const
  32. {
  33.     return rowList.size();
  34. }
  35.  
  36. /*!
  37.     Returns the number of items in the first list item as the number of
  38.     columns in the model. All rows should have the same number of columns.
  39. */
  40.  
  41. int TableModel::columnCount(const QModelIndex &/*parent*/) const
  42. {
  43.     return rowList[0].size();
  44. }
  45.  
  46. /*!
  47.     Returns an appropriate value for the requested data.
  48.     If the view requests an invalid index, an invalid variant is returned.
  49.     Any valid index that corresponds to a string in the list causes that
  50.     string to be returned for the display role; otherwise an invalid variant
  51.     is returned.
  52. */
  53.  
  54. QVariant TableModel::data(const QModelIndex &index, int role) const
  55. {
  56.     if (!index.isValid())
  57.         return QVariant();
  58.  
  59.     if (role == Qt::DisplayRole)
  60.         return rowList[index.row()][index.column()];
  61.     else
  62.         return QVariant();
  63. }
  64.  
  65. /*!
  66.     Returns the appropriate header string depending on the orientation of
  67.     the header and the section. If anything other than the display role is
  68.     requested, we return an invalid variant.
  69. */
  70.  
  71. QVariant TableModel::headerData(int section, Qt::Orientation orientation,
  72.                                 int role) const
  73. {
  74.     if (role != Qt::DisplayRole)
  75.         return QVariant();
  76.  
  77.     if (orientation == Qt::Horizontal)
  78.         return QString("Column %1").arg(section);
  79.     else
  80.         return QString("Row %1").arg(section);
  81. }
  82.  
  83. /*!
  84.     Returns an appropriate value for the item's flags. Valid items are
  85.     enabled, selectable, and editable.
  86. */
  87.  
  88. Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
  89. {
  90.     if (!index.isValid())
  91.         return Qt::ItemIsEnabled;
  92.  
  93.     return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
  94. }
  95.  
  96. /*!
  97.     Changes an item in the model, but only if the following conditions
  98.     are met:
  99.  
  100.     * The index supplied is valid.
  101.     * The role associated with editing text is specified.
  102.  
  103.     The dataChanged() signal is emitted if the item is changed.
  104. */
  105.  
  106. bool TableModel::setData(const QModelIndex &index,
  107.                          const QVariant &value, int role)
  108. {
  109.     if (!index.isValid() || role != Qt::EditRole)
  110.         return false;
  111.  
  112.     qDebug()<< value.toString();
  113.     rowList[index.row()][index.column()] = value.toString();
  114.     emit dataChanged(index, index);
  115.     return true;
  116. }
  117.  
  118. /*!
  119.     Inserts a number of rows into the model at the specified position.
  120. */
  121.  
  122. bool TableModel::insertRows(int position, int rows, const QModelIndex &parent)
  123. {
  124.     int columns = columnCount();
  125.     beginInsertRows(parent, position, position + rows - 1);
  126.  
  127.     for (int row = 0; row < rows; ++row) {
  128.         QStringList items;
  129.         for (int column = 0; column < columns; ++column)
  130.             items.append("");
  131.         rowList.insert(position, items);
  132.     }
  133.  
  134.     endInsertRows();
  135.     return true;
  136. }
  137.  
  138. /*!
  139.     Inserts a number of columns into the model at the specified position.
  140.     Each entry in the list is extended in turn with the required number of
  141.     empty strings.
  142. */
  143.  
  144. bool TableModel::insertColumns(int position, int columns, const QModelIndex &parent)
  145. {
  146.     int rows = rowCount();
  147.     beginInsertColumns(parent, position, position + columns - 1);
  148.  
  149.     for (int row = 0; row < rows; ++row) {
  150.         for (int column = position; column < columns; ++column) {
  151.             rowList[row].insert(position, "");
  152.         }
  153.     }
  154.  
  155.     endInsertColumns();
  156.     return true;
  157. }
  158.  
  159. /*!
  160.     Removes a number of rows from the model at the specified position.
  161. */
  162.  
  163. bool TableModel::removeRows(int position, int rows, const QModelIndex &parent)
  164. {
  165.     beginRemoveRows(parent, position, position + rows - 1);
  166.  
  167.     for (int row = 0; row < rows; ++row) {
  168.         rowList.removeAt(position);
  169.     }
  170.  
  171.     endRemoveRows();
  172.     return true;
  173. }
  174.  
  175. /*!
  176.     Removes a number of columns from the model at the specified position.
  177.     Each row is shortened by the number of columns specified.
  178. */
  179.  
  180. bool TableModel::removeColumns(int position, int columns, const QModelIndex &parent)
  181. {
  182.     int rows = rowCount();
  183.     beginRemoveColumns(parent, position, position + columns - 1);
  184.  
  185.     for (int row = 0; row < rows; ++row) {
  186.         for (int column = 0; column < columns; ++column) {
  187.             rowList[row].removeAt(position);
  188.         }
  189.     }
  190.  
  191.     endRemoveColumns();
  192.     return true;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement