-
Notifications
You must be signed in to change notification settings - Fork 0
/
qt_utils.cpp
131 lines (117 loc) · 3.69 KB
/
qt_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "qt_utils.h"
#include "general_utils.h"
#include <QDebug>
#include <QStringList>
#include <QTableWidget>
#include <QTableView>
#include <QHeaderView>
#include <QStringList>
#include <QStandardItemModel>
#include <QMessageBox>
#include <QString>
QStringList qt_utils::convertToQStringList(std::vector<char*> inputVec)
{
QVector<char*> qVec = QVector<char*>::fromStdVector(inputVec); //Convert first to QVector type
QStringList list;
foreach(const char* str, qVec)
{
list << str;
}
return list;
}
QStringList qt_utils::convertToQStringList(std::vector<std::string> inputVec)
{
std::vector<const char*> constCharVec = general_utils::convertToConstCharVec(inputVec);
QVector<const char*> qVec = QVector<const char*>::fromStdVector(constCharVec); //Convert first to QVector type
QStringList list;
foreach(const char* str, qVec)
{
list << str;
}
return list;
}
void qt_utils::stretchAllColumns(QTableWidget* inputTable)
{
for (int col = 0; col < inputTable->columnCount(); ++col)
{
inputTable->horizontalHeader()->setSectionResizeMode(col, QHeaderView::Stretch);
}
}
void qt_utils::stretchAllColumns(QTableView* inputTable)
{
QAbstractItemModel* tableModel= inputTable->model();
//int iRows = tableModel->rowCount();
int iCols = tableModel->columnCount();
for (int col = 0; col < iCols; ++col)
{
inputTable->horizontalHeader()->setSectionResizeMode(col, QHeaderView::Stretch);
}
}
void qt_utils::resizeRowsToContents(QTableWidget* inputTable)
{
inputTable->resizeRowsToContents();
}
void qt_utils::createInfoBox(const QString& message, const QString& errorString)
{
// Generic use info box
QMessageBox::information(NULL, message, errorString);
}
QString qt_utils::collectTableInfoInStr(QStandardItemModel* inputModel)
{
// Collects all the data from the model
QString textData;
int rows = inputModel->rowCount();
int columns = inputModel->columnCount();
//TODO: Collect the headers of the model too
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
textData += inputModel->data(inputModel->index(i,j)).toString();
textData += ", "; // for .csv file format
}
textData += "\n"; // (optional: for new line segmentation)
}
return textData;
}
QString qt_utils::collectTableInfoInStr(QStandardItemModel* inputModel, std::vector<int>& filteredIndexes)
{
// Collects all the data from the model
QString textData;
int columnCount = inputModel->columnCount();
for (std::vector<int>::iterator itRow = filteredIndexes.begin() ; itRow != filteredIndexes.end(); ++itRow)
{
for (int col = 0; col < columnCount; ++col)
{
textData += inputModel->data(inputModel->index(*itRow,col)).toString();
if(col < columnCount-1)
{
textData += ", "; // for .csv file format
}
}
textData += "\n";
}
return textData;
}
void qt_utils::setTableHeaders(QStandardItemModel* inputModel, QStringList& headers)
{
inputModel->setHorizontalHeaderLabels(headers);
}
QString qt_utils::getTableHeadersInStr(QStandardItemModel* inputModel)
{
int iCols = inputModel->columnCount();
QString headerLabels;
for (int col = 0; col < iCols; ++col)
{
headerLabels += inputModel->headerData(col, Qt::Horizontal, Qt::DisplayRole).toString();
if(col < iCols-1)
{
headerLabels += ", "; // For CSV data
}
}
return headerLabels;
}
QString qt_utils::convertFromStdString(const std::string& inString)
{
return QString::fromStdString(inString);
}