HAL
channel_model.cpp
Go to the documentation of this file.
2 #include <iostream>
4 
5 #define ALL_CHANNEL "all"
6 #define MAX_TEMP_CHANNELS 30
7 
8 namespace hal
9 {
10 
11 
12  ChannelModel::ChannelModel(QObject* parent) : QAbstractTableModel(parent)
13  {
14  mChannelToIgnore = {"UserStudy"};
15  LogManager::get_instance()->get_gui_callback().add_callback("gui",
16  std::bind(&ChannelModel::handleLogmanagerCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
17  }
18 
20  {
21  LogManager::get_instance()->get_gui_callback().remove_callback("gui");
22  }
23 
25  {
26  static ChannelModel s_model;
27  return &s_model;
28  }
29 
30  QVariant ChannelModel::data(const QModelIndex& index, int role) const
31  {
32  if (!index.isValid())
33  return QVariant();
34 
35  if (role != Qt::DisplayRole)
36  return QVariant();
37 
38  ChannelItem* item = static_cast<ChannelItem*>(index.internalPointer());
39  return item->name();
40  }
41 
43  {
44  if (!index.isValid())
45  return 0;
46 
48  }
49 
50  QVariant ChannelModel::headerData(int section, Qt::Orientation orientation, int role) const
51  {
52  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
53  {
54  switch (section)
55  {
56  case static_cast<int>(ColumnNumber::NameColumn):
57  return "Channel";
58  }
59  }
60  return QVariant();
61  }
62 
63  QModelIndex ChannelModel::index(int row, int column, const QModelIndex& parent) const
64  {
65  if (!hasIndex(row, column, parent))
66  return QModelIndex();
67 
68  if (parent.isValid())
69  return QModelIndex();
70  else
71  {
72  if (!mPermanentItems.empty())
73  {
74  if (row <= mPermanentItems.size() - 1)
75  return createIndex(row, column, mPermanentItems.at(row));
76  }
77  if (!mTemporaryItems.empty())
78  {
79  if ((unsigned long)row <= (unsigned long)(mPermanentItems.size() + mTemporaryItems.size() - 1))
80  return createIndex(row, column, mTemporaryItems.at(row - mPermanentItems.size()));
81  }
82  return QModelIndex();
83  }
84  }
85 
86  int ChannelModel::rowCount(const QModelIndex& parent) const
87  {
88  if (parent.isValid())
89  return 0;
90  else
91  return mPermanentItems.size() + mTemporaryItems.size();
92  }
93 
94  int ChannelModel::columnCount(const QModelIndex& parent) const
95  {
96  if (parent.isValid())
97  return 0;
98  else
99  return 2;
100  }
101 
103  {
104  int offset = mPermanentItems.size() + mTemporaryItems.size();
105 
106  if (mTemporaryItems.size() == MAX_TEMP_CHANNELS)
107  {
108  beginRemoveRows(QModelIndex(), offset - 1, offset - 1);
109  delete mTemporaryItems.last();
110  mTemporaryItems.removeLast();
111  endRemoveRows();
112  }
113  ChannelItem* item = new ChannelItem(name);
114 
115  beginInsertRows(QModelIndex(), offset, offset);
116  mTemporaryItems.prepend(item);
117  endInsertRows();
118  return item;
119  }
120 
121  bool ChannelModel::channelExists(const QString& name) const
122  {
123  for (const ChannelItem* item : mPermanentItems)
124  if (item->name() == name) return true;
125  for (const ChannelItem* item : mTemporaryItems)
126  if (item->name() == name) return true;
127  return false;
128  }
129 
130 
131  void ChannelModel::handleLogmanagerCallback(const spdlog::level::level_enum& t, const std::string& channel_name, const std::string& msg_text)
132  {
133  if(mChannelToIgnore.contains(QString::fromStdString(channel_name)))
134  return;
135  if(msg_text == channel_name + " has manually been added to channellist")
136  {
137  if(channelExists(QString::fromStdString(channel_name)))
138  {
139  return;
140  }
141  }
142 
143  ChannelItem* all_channel = nullptr;
144  ChannelItem* item = nullptr;
145  for (auto element : mPermanentItems)
146  {
147  if (element->name().toStdString() == ALL_CHANNEL)
148  {
149  all_channel = element;
150  }
151  if (element->name().toStdString() == channel_name)
152  {
153  item = element;
154  }
155  }
156  if (item == nullptr || all_channel == nullptr)
157  {
158  for (auto element : mTemporaryItems)
159  {
160  if (element->name().toStdString() == ALL_CHANNEL)
161  {
162  all_channel = element;
163  }
164  if (element->name().toStdString() == channel_name)
165  {
166  item = element;
167  }
168  }
169  }
170  if (all_channel == nullptr)
171  {
173  }
174  if (item == nullptr)
175  {
176  item = addChannel(QString::fromStdString(channel_name));
177  }
178 
179  std::string shown_text = msg_text;
180  if (shown_text.length() > 255)
181  {
182  shown_text = shown_text.substr(0, 255) + "...";
183  }
184  all_channel->appendEntry(new ChannelEntry(shown_text, t));
185  item->appendEntry(new ChannelEntry(shown_text, t));
186  Q_EMIT updated(t, ALL_CHANNEL, shown_text);
187  Q_EMIT updated(t, channel_name, shown_text);
188  }
189 } // namespace hal
190 
#define MAX_TEMP_CHANNELS
#define ALL_CHANNEL
This class represents a channel in the channel model. It primarily holds the channel name and its ent...
Definition: channel_item.h:67
void appendEntry(ChannelEntry *entry)
const QString name() const
Table model for log channels.
Definition: channel_model.h:48
void handleLogmanagerCallback(const spdlog::level::level_enum &t, const std::string &channel_name, const std::string &msg_text)
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
void updated(spdlog::level::level_enum t, const std::string &logger_name, std::string const &msg)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
static ChannelModel * instance()
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role) const override
~ChannelModel() override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
ChannelItem * addChannel(const QString name)
CallbackHook< void(const spdlog::level::level_enum &, const std::string &, const std::string &)> & get_gui_callback()
Definition: log.cpp:431
static LogManager * get_instance(const std::filesystem::path &file_name="")
Definition: log.cpp:61
std::string name
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex createIndex(int row, int column, void *ptr) const const
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
bool hasIndex(int row, int column, const QModelIndex &parent) const const
bool contains(const T &value) const const
void * internalPointer() const const
bool isValid() const const
Q_EMITQ_EMIT
QObject * parent() const const
QString fromStdString(const std::string &str)
DisplayRole
typedef ItemFlags
Orientation
The ChannelEntry struct is used by the ChannelItem class to store a single entry.
Definition: channel_item.h:44