Use even less Qt containers

This commit is contained in:
Martin Kleusberg
2019-05-03 14:42:01 +02:00
parent b70e25c786
commit 40aff11086
12 changed files with 61 additions and 64 deletions

View File

@@ -25,17 +25,14 @@
#include <limits>
#if QT_VERSION < QT_VERSION_CHECK(5, 4, 0)
typedef QList<QByteArray> QByteArrayList;
#endif
QList<QByteArrayList> ExtendedTableWidget::m_buffer;
using BufferRow = std::vector<QByteArray>;
std::vector<BufferRow> ExtendedTableWidget::m_buffer;
QString ExtendedTableWidget::m_generatorStamp;
namespace
{
QList<QByteArrayList> parseClipboard(QString clipboard)
std::vector<BufferRow> parseClipboard(QString clipboard)
{
// Remove trailing line break from the clipboard text. This is necessary because some applications append an extra
// line break to the clipboard contents which we would then interpret as regular data, setting the first field of the
@@ -50,11 +47,11 @@ QList<QByteArrayList> parseClipboard(QString clipboard)
clipboard.chop(1);
// Make sure there is some data in the clipboard
QList<QByteArrayList> result;
std::vector<BufferRow> result;
if(clipboard.isEmpty())
return result;
result.push_back(QByteArrayList());
result.push_back(BufferRow());
QRegExp re("(\"(?:[^\t\"]+|\"\"[^\"]*\"\")*)\"|(\t|\r?\n)");
int offset = 0;
@@ -69,7 +66,7 @@ QList<QByteArrayList> parseClipboard(QString clipboard)
if(QRegExp("\".*\"").exactMatch(text))
text = text.mid(1, text.length() - 2);
text.replace("\"\"", "\"");
result.last().push_back(text.toUtf8());
result.back().push_back(text.toUtf8());
break;
}
@@ -81,18 +78,18 @@ QList<QByteArrayList> parseClipboard(QString clipboard)
QString ws = re.cap(2);
// if two whitespaces in row - that's an empty cell
if (!(pos - whitespace_offset)) {
result.last().push_back(QByteArray());
result.back().push_back(QByteArray());
} else {
text = clipboard.mid(whitespace_offset, pos - whitespace_offset);
if(QRegExp("\".*\"").exactMatch(text))
text = text.mid(1, text.length() - 2);
text.replace("\"\"", "\"");
result.last().push_back(text.toUtf8());
result.back().push_back(text.toUtf8());
}
if (ws.endsWith("\n"))
// create new row
result.push_back(QByteArrayList());
result.push_back(BufferRow());
whitespace_offset = offset = pos + ws.length();
}
@@ -440,7 +437,7 @@ void ExtendedTableWidget::copyMimeData(const QModelIndexList& fromIndices, QMime
// Copy selected data into internal copy-paste buffer
int last_row = indices.first().row();
QByteArrayList lst;
BufferRow lst;
for(int i=0;i<indices.size();i++)
{
if(indices.at(i).row() != last_row)
@@ -448,7 +445,7 @@ void ExtendedTableWidget::copyMimeData(const QModelIndexList& fromIndices, QMime
m_buffer.push_back(lst);
lst.clear();
}
lst << indices.at(i).data(Qt::EditRole).toByteArray();
lst.push_back(indices.at(i).data(Qt::EditRole).toByteArray());
last_row = indices.at(i).row();
}
m_buffer.push_back(lst);
@@ -610,10 +607,10 @@ void ExtendedTableWidget::paste()
// If data in system clipboard is ours and the internal copy-paste buffer is filled, use the internal buffer; otherwise parse the
// system clipboard contents (case for data copied by other application).
QList<QByteArrayList> clipboardTable;
QList<QByteArrayList>* source;
std::vector<BufferRow> clipboardTable;
std::vector<BufferRow>* source;
if(mimeClipboard->hasHtml() && mimeClipboard->html().contains(m_generatorStamp) && !m_buffer.isEmpty())
if(mimeClipboard->hasHtml() && mimeClipboard->html().contains(m_generatorStamp) && !m_buffer.empty())
{
source = &m_buffer;
} else {
@@ -626,8 +623,8 @@ void ExtendedTableWidget::paste()
return;
// Starting from assumption that selection is rectangular, and then first index is upper-left corner and last is lower-right.
int rows = source->size();
int columns = source->first().size();
int rows = static_cast<int>(source->size());
int columns = static_cast<int>(source->front().size());
int firstRow = indices.front().row();
int firstColumn = indices.front().column();
@@ -641,7 +638,7 @@ void ExtendedTableWidget::paste()
// Special case: if there is only one cell of data to be pasted, paste it into all selected fields
if(rows == 1 && columns == 1)
{
QByteArray data = source->first().first();
QByteArray data = source->front().front();
for(int row=firstRow;row<firstRow+selectedRows;row++)
{
for(int column=firstColumn;column<firstColumn+selectedColumns;column++)
@@ -667,7 +664,7 @@ void ExtendedTableWidget::paste()
// Copy the data cell by cell and as-is from the source buffer to the table
int row = firstRow;
for(const QByteArrayList& source_row : *source)
for(const auto& source_row : *source)
{
int column = firstColumn;
for(const QByteArray& source_cell : source_row)

View File

@@ -79,8 +79,7 @@ private:
void useAsFilter(const QString& filterOperator, bool binary = false, const QString& operatorSuffix = "");
void duplicateUpperCell();
typedef QList<QByteArray> QByteArrayList;
static QList<QByteArrayList> m_buffer;
static std::vector<std::vector<QByteArray>> m_buffer;
static QString m_generatorStamp;
private slots:

View File

@@ -1,6 +1,8 @@
#include "FileExtensionManager.h"
#include "ui_FileExtensionManager.h"
#include <set>
FileExtensionManager::FileExtensionManager(QStringList init, QWidget *parent) :
QDialog(parent),
ui(new Ui::FileExtensionManager)
@@ -46,21 +48,12 @@ void FileExtensionManager::addItem()
void FileExtensionManager::removeItem()
{
QList<int> selectedRows;
std::set<int> selectedRows;
for (const QTableWidgetItem* item : ui->tableExtensions->selectedItems())
{
if (selectedRows.contains(item->row()) == false)
{
selectedRows.append(item->row());
}
}
selectedRows.insert(item->row());
qSort(selectedRows);
for (int i = selectedRows.size()-1; i >= 0; --i)
{
ui->tableExtensions->removeRow(selectedRows[i]);
}
for(int row : selectedRows)
ui->tableExtensions->removeRow(row);
}
void FileExtensionManager::upItem()

View File

@@ -671,7 +671,7 @@ void MainWindow::populateStructure(const QString& old_table)
sqlb::FieldInfoList fi = jt->fieldInformation();
for(const sqlb::FieldInfo& f : fi)
tablesToColumnsMap[objectname].append(QString::fromStdString(f.name));
tablesToColumnsMap[objectname].push_back(QString::fromStdString(f.name));
}
qualifiedTablesMap[QString::fromStdString(it.key())] = tablesToColumnsMap;
}

View File

@@ -76,7 +76,7 @@ void RemoteDatabase::reloadSettings()
file.open(QFile::ReadOnly);
QSslCertificate cert(&file);
file.close();
m_clientCertFiles.insert(path, cert);
m_clientCertFiles.insert({path, cert});
}
// Always add the default certificate for anonymous access to dbhub.io
@@ -85,7 +85,7 @@ void RemoteDatabase::reloadSettings()
file.open(QFile::ReadOnly);
QSslCertificate cert(&file);
file.close();
m_clientCertFiles.insert(":/user_certs/public.cert.pem", cert);
m_clientCertFiles.insert({":/user_certs/public.cert.pem", cert});
}
// TODO Add support for proxies here
@@ -200,9 +200,9 @@ void RemoteDatabase::gotReply(QNetworkReply* reply)
break;
// Parse data and build licence map (short name -> long name)
QMap<std::string, std::string> licences;
std::map<std::string, std::string> licences;
for(auto it=obj.cbegin();it!=obj.cend();++it)
licences.insert(it.key(), it.value()["full_name"]);
licences.insert({it.key(), it.value()["full_name"]});
// Send licence map to anyone who's interested
emit gotLicenceList(licences);
@@ -318,7 +318,7 @@ const QList<QSslCertificate>& RemoteDatabase::caCertificates() const
QString RemoteDatabase::getInfoFromClientCert(const QString& cert, CertInfo info) const
{
// Get the common name of the certificate and split it into user name and server address
QString cn = m_clientCertFiles[cert].subjectInfo(QSslCertificate::CommonName).at(0);
QString cn = m_clientCertFiles.at(cert).subjectInfo(QSslCertificate::CommonName).at(0);
QStringList cn_parts = cn.split("@");
if(cn_parts.size() < 2)
return QString();

View File

@@ -4,6 +4,8 @@
#include <QObject>
#include <QtNetwork/QSslConfiguration>
#include <map>
class QNetworkAccessManager;
class QNetworkConfigurationManager;
class QNetworkReply;
@@ -30,7 +32,7 @@ public:
};
const QList<QSslCertificate>& caCertificates() const;
const QMap<QString, QSslCertificate>& clientCertificates() const { return m_clientCertFiles; }
const std::map<QString, QSslCertificate>& clientCertificates() const { return m_clientCertFiles; }
QString getInfoFromClientCert(const QString& cert, CertInfo info) const;
enum RequestType
@@ -61,7 +63,7 @@ signals:
// a directory listing or the licence list.
void gotDirList(QString json, QVariant userdata);
void gotCurrentVersion(QString version, QString url);
void gotLicenceList(QMap<std::string, std::string> licences);
void gotLicenceList(std::map<std::string, std::string> licences);
void gotBranchList(std::vector<std::string> branches, std::string default_branch);
// The uploadFinished() signal is emitted when a push() call is finished, i.e. a database upload has completed.
@@ -94,7 +96,7 @@ private:
QNetworkConfigurationManager* m_configurationManager;
QProgressDialog* m_progress;
QSslConfiguration m_sslConfiguration;
QMap<QString, QSslCertificate> m_clientCertFiles;
std::map<QString, QSslCertificate> m_clientCertFiles;
sqlite3* m_dbLocal;
};

View File

@@ -28,12 +28,12 @@ void RemoteModelItem::setValue(RemoteModelColumns column, QVariant value)
void RemoteModelItem::appendChild(RemoteModelItem *item)
{
m_children.append(item);
m_children.push_back(item);
}
RemoteModelItem* RemoteModelItem::child(int row) const
{
return m_children.value(row);
return m_children[static_cast<size_t>(row)];
}
RemoteModelItem* RemoteModelItem::parent() const
@@ -43,13 +43,19 @@ RemoteModelItem* RemoteModelItem::parent() const
int RemoteModelItem::childCount() const
{
return m_children.count();
return static_cast<int>(m_children.size());
}
int RemoteModelItem::row() const
{
if(m_parent)
return m_parent->m_children.indexOf(const_cast<RemoteModelItem*>(this));
{
auto f = std::find( m_parent->m_children.begin(), m_parent->m_children.end(), const_cast<RemoteModelItem*>(this));
if(f == m_parent->m_children.end())
return -1;
else
return static_cast<int>(std::distance(m_parent->m_children.begin(), f));
}
return 0;
}

View File

@@ -48,7 +48,7 @@ private:
QVariant m_values[RemoteModelColumnCount];
// Child items and parent item
QList<RemoteModelItem*> m_children;
std::vector<RemoteModelItem*> m_children;
RemoteModelItem* m_parent;
// Indicates whether we already tried fetching a directory listing for this item. This serves two purposes:

View File

@@ -104,15 +104,15 @@ bool RemotePushDialog::forcePush() const
return ui->checkForce->isChecked();
}
void RemotePushDialog::fillInLicences(const QMap<std::string, std::string>& licences)
void RemotePushDialog::fillInLicences(const std::map<std::string, std::string>& licences)
{
// Clear licence list and add default item for unspecified licence
ui->comboLicence->clear();
ui->comboLicence->addItem(tr("Unspecified"), QString());
// Parse licence list and fill combo box. Show the full name to the user and use the short name as user data.
for(auto it=licences.constBegin();it!=licences.constEnd();++it)
ui->comboLicence->addItem(QString::fromStdString(it.value()), QString::fromStdString(it.key()));
for(const auto& it : licences)
ui->comboLicence->addItem(QString::fromStdString(it.second), QString::fromStdString(it.first));
}
void RemotePushDialog::fillInBranches(const std::vector<std::string>& branches, const std::string& default_branch)

View File

@@ -45,7 +45,7 @@ protected slots:
void reloadBranchList();
void fillInLicences(const QMap<std::string, std::string>& licences);
void fillInLicences(const std::map<std::string, std::string>& licences);
void fillInBranches(const std::vector<std::string>& branches, const std::string& default_branch);
};

View File

@@ -155,24 +155,24 @@ void SqlUiLexer::setTableNames(const QualifiedTablesMap& tables)
autocompleteApi->clear();
listTables.clear();
setupAutoCompletion();
for(auto itSchemas=tables.constBegin();itSchemas!=tables.constEnd();++itSchemas)
for(const auto& itSchemas : tables)
{
for(auto itTables=itSchemas.value().constBegin();itTables!=itSchemas.value().constEnd();++itTables)
for(const auto& itTables : itSchemas.second)
{
// Completion for schema.table
autocompleteApi->add(itSchemas.key() + "?" + QString::number(SqlUiLexer::ApiCompleterIconIdSchema) + "." +
itTables.key() + "?" + QString::number(SqlUiLexer::ApiCompleterIconIdTable));
autocompleteApi->add(itSchemas.first + "?" + QString::number(SqlUiLexer::ApiCompleterIconIdSchema) + "." +
itTables.first + "?" + QString::number(SqlUiLexer::ApiCompleterIconIdTable));
for(const QString& field : itTables.value()) {
for(const QString& field : itTables.second) {
// Completion for table.field
autocompleteApi->add(itTables.key() + "?" + QString::number(SqlUiLexer::ApiCompleterIconIdTable) + "." +
autocompleteApi->add(itTables.first + "?" + QString::number(SqlUiLexer::ApiCompleterIconIdTable) + "." +
field + "?" + QString::number(SqlUiLexer::ApiCompleterIconIdColumn));
// Completion for isolated field
autocompleteApi->add(field + "?" + QString::number(SqlUiLexer::ApiCompleterIconIdColumn));
}
// Store the table name list in order to highlight them in a different colour
listTables.append(itTables.key());
listTables.append(itTables.first);
}
}
autocompleteApi->prepare();

View File

@@ -3,7 +3,7 @@
#include "Qsci/qscilexersql.h"
#include <QMap>
#include <map>
class QsciAPIs;
@@ -23,8 +23,8 @@ public:
ApiCompleterIconIdSchema,
};
typedef QMap<QString, QList<QString> > TablesAndColumnsMap;
typedef QMap<QString, TablesAndColumnsMap > QualifiedTablesMap;
using TablesAndColumnsMap = std::map<QString, std::vector<QString>>;
using QualifiedTablesMap = std::map<QString, TablesAndColumnsMap>;
void setTableNames(const QualifiedTablesMap& tables);