2017-03-29 10:18:37 +00:00
|
|
|
#include "newfiledialog.h"
|
|
|
|
#include "optionsdialog.h"
|
|
|
|
#include "ui_newfiledialog.h"
|
|
|
|
#include "createnewdialog.h"
|
|
|
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QtGui>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QDir>
|
|
|
|
|
2017-04-01 11:20:13 +00:00
|
|
|
const int NewFileDialog::MaxRecentFiles;
|
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
static QColor getColorFor(QString str, int pos)
|
|
|
|
{
|
2017-07-02 11:11:02 +00:00
|
|
|
IAITONOTUSED(str);
|
2017-04-09 17:09:35 +00:00
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
QList<QColor> Colors;
|
|
|
|
Colors << QColor(29, 188, 156); // Turquoise
|
|
|
|
Colors << QColor(52, 152, 219); // Blue
|
|
|
|
Colors << QColor(155, 89, 182); // Violet
|
|
|
|
Colors << QColor(52, 73, 94); // Grey
|
|
|
|
Colors << QColor(231, 76, 60); // Red
|
|
|
|
Colors << QColor(243, 156, 17); // Orange
|
|
|
|
|
|
|
|
return Colors[pos % 6];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
static QIcon getIconFor(QString str, int pos)
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
// Add to the icon list
|
|
|
|
int w = 64;
|
|
|
|
int h = 64;
|
2017-04-01 11:20:13 +00:00
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
QPixmap pixmap(w, h);
|
2017-04-01 09:51:32 +00:00
|
|
|
pixmap.fill(Qt::transparent);
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-04-01 09:51:32 +00:00
|
|
|
QPainter pixPaint(&pixmap);
|
|
|
|
pixPaint.setPen(Qt::NoPen);
|
2017-03-29 10:18:37 +00:00
|
|
|
pixPaint.setRenderHint(QPainter::Antialiasing);
|
|
|
|
pixPaint.setBrush(QBrush(QBrush(getColorFor(str, pos))));
|
2017-04-09 19:55:06 +00:00
|
|
|
pixPaint.drawEllipse(1, 1, w - 2, h - 2);
|
2017-03-29 10:18:37 +00:00
|
|
|
pixPaint.setPen(Qt::white);
|
2017-04-09 19:55:06 +00:00
|
|
|
pixPaint.setFont(QFont("Verdana", 24, 1));
|
|
|
|
pixPaint.drawText(0, 0, w, h - 2, Qt::AlignCenter, QString(str).toUpper().mid(0, 2));
|
2017-03-29 10:18:37 +00:00
|
|
|
return QIcon(pixmap);
|
|
|
|
}
|
|
|
|
NewFileDialog::NewFileDialog(QWidget *parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::NewFileDialog)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2017-03-31 00:51:14 +00:00
|
|
|
setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
|
2017-03-29 10:18:37 +00:00
|
|
|
ui->recentsList->addAction(ui->actionRemove_item);
|
|
|
|
ui->recentsList->addAction(ui->actionClear_all);
|
|
|
|
ui->recentsList->setIconSize(QSize(48, 48));
|
|
|
|
|
|
|
|
// Fill list with recent opened files
|
2017-04-03 00:18:09 +00:00
|
|
|
QSettings settings;
|
2017-03-29 10:18:37 +00:00
|
|
|
|
|
|
|
QStringList files = settings.value("recentFileList").toStringList();
|
|
|
|
|
|
|
|
int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
|
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
for (int i = 0; i < numRecentFiles; ++i)
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
// Get stored files
|
|
|
|
//QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
|
|
|
|
|
|
|
|
// Remove all but the file name
|
|
|
|
QString sep = QDir::separator();
|
2017-04-09 19:55:06 +00:00
|
|
|
QStringList name_list = files[i].split(sep);
|
2017-03-29 10:18:37 +00:00
|
|
|
QString name = name_list.last();
|
|
|
|
|
|
|
|
// Get file info
|
|
|
|
QFileInfo info(files[i]);
|
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
QListWidgetItem *item = new QListWidgetItem(getIconFor(name, i), files[i] + "\nCreated: " + info.created().toString() + "\nSize: " + QString::number(info.size()));
|
2017-06-15 09:53:10 +00:00
|
|
|
//":/img/icons/target.svg"), name );
|
2017-03-29 10:18:37 +00:00
|
|
|
item->setData(Qt::UserRole, files[i]);
|
|
|
|
ui->recentsList->addItem(item);
|
|
|
|
}
|
|
|
|
ui->recentsList->setSortingEnabled(true);
|
|
|
|
|
|
|
|
// Hide "create" button until the dialog works
|
|
|
|
ui->createButton->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
NewFileDialog::~NewFileDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::on_loadFileButton_clicked()
|
|
|
|
{
|
|
|
|
// Check that there is a file selected
|
|
|
|
QString fname = ui->newFileEdit->text();
|
2017-04-04 21:00:55 +00:00
|
|
|
QFileInfo checkfile(fname);
|
2017-04-09 19:55:06 +00:00
|
|
|
if (!checkfile.exists() || !checkfile.isFile())
|
|
|
|
{
|
2017-04-09 20:36:17 +00:00
|
|
|
QMessageBox msgBox(this);
|
2017-03-29 10:18:37 +00:00
|
|
|
msgBox.setText("Select a new program or a previous one\nbefore continue");
|
|
|
|
msgBox.exec();
|
2017-04-09 19:55:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-04 21:00:55 +00:00
|
|
|
// Add file to recent file list
|
|
|
|
QSettings settings;
|
|
|
|
QStringList files = settings.value("recentFileList").toStringList();
|
|
|
|
files.removeAll(fname);
|
|
|
|
files.prepend(fname);
|
|
|
|
while (files.size() > MaxRecentFiles)
|
|
|
|
files.removeLast();
|
|
|
|
|
|
|
|
settings.setValue("recentFileList", files);
|
2017-04-09 19:55:06 +00:00
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
close();
|
2017-05-13 18:09:36 +00:00
|
|
|
|
|
|
|
// Close dialog and open MainWindow/OptionsDialog
|
|
|
|
MainWindow *main = new MainWindow();
|
|
|
|
main->openFile(fname);
|
|
|
|
//OptionsDialog *o = new OptionsDialog(fname);
|
|
|
|
//o->exec();
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::on_newFileButton_clicked()
|
|
|
|
{
|
|
|
|
QFileDialog dialog(this);
|
|
|
|
dialog.setFileMode(QFileDialog::ExistingFile);
|
|
|
|
dialog.setViewMode(QFileDialog::Detail);
|
|
|
|
dialog.setDirectory(QDir::home());
|
|
|
|
|
|
|
|
QString fileName;
|
|
|
|
fileName = dialog.getOpenFileName(this, "Select file");
|
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
if (!fileName.isEmpty())
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
ui->newFileEdit->setText(fileName);
|
2017-04-09 19:55:06 +00:00
|
|
|
ui->loadFileButton->setFocus();
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::on_recentsList_itemClicked(QListWidgetItem *item)
|
|
|
|
{
|
2017-04-09 19:55:06 +00:00
|
|
|
QVariant data = item->data(Qt::UserRole);
|
2017-03-29 10:18:37 +00:00
|
|
|
QString sitem = data.toString();
|
|
|
|
ui->newFileEdit->setText(sitem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::on_recentsList_itemDoubleClicked(QListWidgetItem *item)
|
|
|
|
{
|
|
|
|
// Get selected item to send to options dialog
|
2017-04-09 19:55:06 +00:00
|
|
|
QVariant data = item->data(Qt::UserRole);
|
2017-03-29 10:18:37 +00:00
|
|
|
QString sitem = data.toString();
|
|
|
|
// Close dialog and open OptionsDialog
|
|
|
|
close();
|
2017-05-13 18:09:36 +00:00
|
|
|
|
|
|
|
MainWindow *main = new MainWindow();
|
|
|
|
main->openFile(sitem);
|
|
|
|
//OptionsDialog *o = new OptionsDialog(sitem);
|
|
|
|
//o->exec();
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::on_cancelButton_clicked()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::on_actionRemove_item_triggered()
|
|
|
|
{
|
|
|
|
// Remove selected item from recents list
|
2017-04-09 19:55:06 +00:00
|
|
|
QListWidgetItem *item = ui->recentsList->currentItem();
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
QVariant data = item->data(Qt::UserRole);
|
2017-03-29 10:18:37 +00:00
|
|
|
QString sitem = data.toString();
|
|
|
|
|
2017-04-03 00:18:09 +00:00
|
|
|
QSettings settings;
|
2017-03-29 10:18:37 +00:00
|
|
|
QStringList files = settings.value("recentFileList").toStringList();
|
|
|
|
files.removeAll(sitem);
|
|
|
|
settings.setValue("recentFileList", files);
|
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
ui->recentsList->takeItem(ui->recentsList->currentRow());
|
2017-03-29 10:18:37 +00:00
|
|
|
|
|
|
|
ui->newFileEdit->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::on_createButton_clicked()
|
|
|
|
{
|
|
|
|
// Close dialog and open create new file dialog
|
|
|
|
close();
|
2017-04-09 20:36:17 +00:00
|
|
|
createNewDialog *n = new createNewDialog(nullptr);
|
2017-03-29 10:18:37 +00:00
|
|
|
n->exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::on_actionClear_all_triggered()
|
|
|
|
{
|
|
|
|
// Clear recent file list
|
2017-04-03 00:18:09 +00:00
|
|
|
QSettings settings;
|
2017-03-29 10:18:37 +00:00
|
|
|
QStringList files = settings.value("recentFileList").toStringList();
|
|
|
|
files.clear();
|
|
|
|
|
|
|
|
ui->recentsList->clear();
|
|
|
|
// TODO: if called from main window its ok, otherwise its not
|
|
|
|
settings.setValue("recentFileList", files);
|
|
|
|
ui->newFileEdit->clear();
|
|
|
|
}
|