swift
actionhotkeylistmodel.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QString>
7 #include <QtGlobal>
8 
9 #include "misc/identifier.h"
12 #include "misc/sequence.h"
13 
14 using namespace swift::misc;
15 using namespace swift::misc::input;
16 
17 namespace swift::gui::models
18 {
19  CActionHotkeyListModel::CActionHotkeyListModel(QObject *parent) : QAbstractTableModel(parent) {}
20 
21  int CActionHotkeyListModel::rowCount(const QModelIndex & ) const { return m_actionHotkeys.size(); }
22 
23  int CActionHotkeyListModel::columnCount(const QModelIndex & ) const { return 3; }
24 
25  QVariant CActionHotkeyListModel::data(const QModelIndex &index, int role) const
26  {
27  if (!index.isValid()) { return QVariant(); }
28  if (index.row() >= m_actionHotkeys.size() || index.row() < 0) { return QVariant(); }
29 
30  if (role == Qt::DisplayRole)
31  {
32  const int col = index.column();
33  if (col == 0)
34  {
35  const CIdentifier identifier = m_actionHotkeys[index.row()].getApplicableMachine();
36  return identifier.getMachineName();
37  }
38  if (col == 1)
39  {
40  const CHotkeyCombination combination = m_actionHotkeys[index.row()].getCombination();
41  return combination.asStringWithDeviceNames();
42  // return combination.toQString();
43  }
44  if (col == 2) { return m_actionHotkeys[index.row()].getAction(); }
45  }
46  else if (role == ActionHotkeyRole)
47  {
48  const auto hotkey = m_actionHotkeys[index.row()];
49  return QVariant::fromValue(hotkey);
50  }
51  return {};
52  }
53 
54  QVariant CActionHotkeyListModel::headerData(int section, Qt::Orientation orientation, int role) const
55  {
56  if (role == Qt::DisplayRole)
57  {
58  if (orientation == Qt::Horizontal)
59  {
60  switch (section)
61  {
62  case 0: return QStringLiteral("Machine");
63  case 1: return QStringLiteral("Combination");
64  case 2: return QStringLiteral("Action");
65  }
66  }
67  }
68  return {};
69  }
70 
71  bool CActionHotkeyListModel::insertRows(int position, int rows, const QModelIndex &index)
72  {
73  Q_UNUSED(index)
74  beginInsertRows(QModelIndex(), position, position + rows - 1);
75 
76  for (int row = 0; row < rows; ++row) { m_actionHotkeys.push_back(swift::misc::input::CActionHotkey()); }
77 
78  endInsertRows();
79  return true;
80  }
81 
82  bool CActionHotkeyListModel::removeRows(int position, int rows, const QModelIndex &index)
83  {
84  Q_UNUSED(index)
85  beginRemoveRows(QModelIndex(), position, position + rows - 1);
86 
87  Q_ASSERT(position + rows - 1 < m_actionHotkeys.size());
88 
89  for (int row = 0; row < rows; ++row)
90  {
91  auto toRemove = m_actionHotkeys[position + row];
92  m_actionHotkeys.remove(toRemove);
93  }
94 
95  endRemoveRows();
96  return true;
97  }
98 
99  bool CActionHotkeyListModel::setData(const QModelIndex &index, const QVariant &var, int role)
100  {
101  if (index.isValid() && role == ActionHotkeyRole)
102  {
103  m_actionHotkeys[index.row()] = var.value<swift::misc::input::CActionHotkey>();
104  emit dataChanged(index, index);
105  return true;
106  }
107  return false;
108  }
109 
111  {
112  beginResetModel();
113  m_actionHotkeys.clear();
114  endResetModel();
115  }
116 } // namespace swift::gui::models
bool removeRows(int position, int rows, const QModelIndex &index)
QVariant headerData(int section, Qt::Orientation orientation, int role) const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
int rowCount(const QModelIndex &parent=QModelIndex()) const
bool setData(const QModelIndex &index, const QVariant &var, int role)
int columnCount(const QModelIndex &parent=QModelIndex()) const
bool insertRows(int position, int rows, const QModelIndex &index)
Value object encapsulating information identifying a component of a modular distributed swift process...
Definition: identifier.h:29
const QString & getMachineName() const
Machine name.
Definition: identifier.h:103
size_type size() const
Returns number of elements in the sequence.
Definition: sequence.h:273
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
void clear()
Removes all elements in the sequence.
Definition: sequence.h:288
int remove(const T &object)
Remove all elements equal to the given object, if it is contained.
Definition: sequence.h:435
Value object encapsulating a action hotkey.
Definition: actionhotkey.h:25
Value object representing hotkey sequence.
QString asStringWithDeviceNames() const
Returns the button name with the device name prefix.
Models to be used with views, mainly QTableView.
Free functions in swift::misc.