2021-09-04 16:34:14 +08:00
|
|
|
#include "compilerautolinkwidget.h"
|
|
|
|
#include "ui_compilerautolinkwidget.h"
|
|
|
|
#include "../mainwindow.h"
|
|
|
|
#include "../settings.h"
|
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
CompilerAutolinkWidget::CompilerAutolinkWidget(const QString& name, const QString& group, QWidget* parent) :
|
|
|
|
SettingsWidget(name,group,parent),
|
2021-09-04 19:27:44 +08:00
|
|
|
mModel(this),
|
2021-09-04 16:34:14 +08:00
|
|
|
ui(new Ui::CompilerAutolinkWidget)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
ui->tblAutolinks->setModel(&mModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
CompilerAutolinkWidget::~CompilerAutolinkWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerAutolinkWidget::doLoad()
|
|
|
|
{
|
|
|
|
ui->grpAutolink->setChecked(pSettings->editor().enableAutolink());
|
|
|
|
mModel.setLinks(pAutolinkManager->links());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerAutolinkWidget::doSave()
|
|
|
|
{
|
|
|
|
pSettings->editor().setEnableAutolink(ui->grpAutolink->isChecked());
|
|
|
|
pSettings->editor().save();
|
|
|
|
pAutolinkManager->clear();
|
2021-09-04 19:27:44 +08:00
|
|
|
for (const PAutolink& link:mModel.links()) {
|
|
|
|
if (!link->header.isEmpty()) {
|
|
|
|
pAutolinkManager->setLink(
|
|
|
|
link->header,
|
|
|
|
link->linkOption
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try{
|
|
|
|
pAutolinkManager->save();
|
|
|
|
} catch (FileError e) {
|
|
|
|
QMessageBox::critical(this,
|
|
|
|
tr("Save failed."),
|
|
|
|
e.reason(),
|
|
|
|
QMessageBox::Ok);
|
2021-09-04 16:34:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:27:44 +08:00
|
|
|
AutolinkModel::AutolinkModel(CompilerAutolinkWidget* widget,QObject *parent):
|
|
|
|
QAbstractTableModel(parent),
|
|
|
|
mWidget(widget)
|
2021-09-04 16:34:14 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int AutolinkModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return mLinks.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int AutolinkModel::columnCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant AutolinkModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
|
|
switch(section) {
|
|
|
|
case 0:
|
|
|
|
return tr("Header");
|
|
|
|
case 1:
|
|
|
|
return tr("Link options");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant AutolinkModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
|
|
|
int row = index.row();
|
2021-09-04 19:27:44 +08:00
|
|
|
if (row<0 || row>=mLinks.count())
|
2021-09-04 16:34:14 +08:00
|
|
|
return QVariant();
|
2021-09-04 19:27:44 +08:00
|
|
|
PAutolink link = mLinks[row];
|
2021-09-04 16:34:14 +08:00
|
|
|
switch(index.column()) {
|
|
|
|
case 0:
|
|
|
|
return link->header;
|
|
|
|
case 1:
|
|
|
|
return link->linkOption;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutolinkModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return false;
|
|
|
|
if (role == Qt::EditRole) {
|
|
|
|
int row = index.row();
|
2021-09-04 19:27:44 +08:00
|
|
|
if (row<0 || row>=mLinks.count())
|
2021-09-04 16:34:14 +08:00
|
|
|
return false;
|
2021-09-04 19:27:44 +08:00
|
|
|
PAutolink link = mLinks[row];
|
|
|
|
QString s=value.toString().trimmed();
|
2021-09-04 16:34:14 +08:00
|
|
|
if (index.column() == 0) {
|
|
|
|
if (s.isEmpty())
|
|
|
|
return false;
|
2021-09-04 19:27:44 +08:00
|
|
|
if (findLink(s)>=0) {
|
2021-09-04 16:34:14 +08:00
|
|
|
QMessageBox::warning(pMainWindow,
|
|
|
|
tr("Header exists"),
|
|
|
|
tr("Header already exists."),
|
2021-09-04 19:27:44 +08:00
|
|
|
QMessageBox::Ok);
|
2021-09-04 16:34:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-04 19:27:44 +08:00
|
|
|
//we must create a new link, becasue mList may share link pointer with the autolink manger
|
2021-09-04 16:34:14 +08:00
|
|
|
PAutolink newLink = std::make_shared<Autolink>();
|
|
|
|
newLink->header = s;
|
|
|
|
newLink->linkOption = link->linkOption;
|
2021-09-04 19:27:44 +08:00
|
|
|
mLinks[row]=newLink;
|
|
|
|
mWidget->setSettingsChanged();
|
2021-09-04 16:34:14 +08:00
|
|
|
return true;
|
|
|
|
} else if (index.column() == 1) {
|
2021-09-04 19:27:44 +08:00
|
|
|
//we must create a new link, becasue mList may share link pointer with the autolink manger
|
2021-09-04 16:34:14 +08:00
|
|
|
PAutolink newLink = std::make_shared<Autolink>();
|
|
|
|
newLink->header = link->header;
|
|
|
|
newLink->linkOption = s;
|
2021-09-04 19:27:44 +08:00
|
|
|
mLinks[row]=newLink;
|
|
|
|
mWidget->setSettingsChanged();
|
2021-09-04 16:34:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags AutolinkModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
Qt::ItemFlags flags = Qt::NoItemFlags;
|
|
|
|
if (index.isValid()) {
|
|
|
|
flags = Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable ;
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:27:44 +08:00
|
|
|
bool AutolinkModel::insertRows(int row, int count, const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
if (row<0 || row>mLinks.count())
|
|
|
|
return false;
|
|
|
|
beginInsertRows(parent,row,row+count-1);
|
|
|
|
for (int i=row;i<row+count;i++) {
|
|
|
|
PAutolink link = std::make_shared<Autolink>();
|
|
|
|
mLinks.insert(i,link);
|
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AutolinkModel::removeRows(int row, int count, const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
if (row<0 || row>=mLinks.count())
|
|
|
|
return false;
|
|
|
|
beginRemoveRows(parent,row,row+count-1);
|
|
|
|
for (int i=row;i<row+count;i++) {
|
|
|
|
if (i>=mLinks.count())
|
|
|
|
break;
|
|
|
|
mLinks.removeAt(i);
|
|
|
|
}
|
|
|
|
endRemoveRows();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const QList<PAutolink> &AutolinkModel::links() const
|
2021-09-04 16:34:14 +08:00
|
|
|
{
|
|
|
|
return mLinks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutolinkModel::setLinks(const QMap<QString, PAutolink> &newLinks)
|
|
|
|
{
|
2021-09-04 19:27:44 +08:00
|
|
|
mLinks = newLinks.values();
|
2021-09-04 16:34:14 +08:00
|
|
|
}
|
2021-09-04 19:27:44 +08:00
|
|
|
|
|
|
|
int AutolinkModel::findLink(const QString &header)
|
|
|
|
{
|
|
|
|
for (int i=0;i<mLinks.count();i++) {
|
|
|
|
PAutolink link = mLinks[i];
|
|
|
|
if (link->header == header)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerAutolinkWidget::on_btnAdd_pressed()
|
|
|
|
{
|
|
|
|
mModel.insertRow(mModel.links().count());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CompilerAutolinkWidget::on_btnRemove_pressed()
|
|
|
|
{
|
|
|
|
QModelIndex index = ui->tblAutolinks->currentIndex();
|
|
|
|
mModel.removeRow(index.row(),index.parent());
|
|
|
|
}
|
|
|
|
|