2017-10-01 19:09:42 +00:00
|
|
|
#include "OptionsDialog.h"
|
2017-10-02 09:41:28 +00:00
|
|
|
#include "dialogs/CreateNewDialog.h"
|
|
|
|
#include "dialogs/NewFileDialog.h"
|
2017-12-04 21:38:09 +00:00
|
|
|
#include "dialogs/AboutDialog.h"
|
2017-10-01 19:09:42 +00:00
|
|
|
#include "ui_NewfileDialog.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QtGui>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QDir>
|
|
|
|
|
2017-04-01 11:20:13 +00:00
|
|
|
const int NewFileDialog::MaxRecentFiles;
|
|
|
|
|
2017-10-01 18:08:12 +00:00
|
|
|
static QColor getColorFor(const QString& str, int pos)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-10-01 18:08:12 +00:00
|
|
|
Q_UNUSED(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-10-01 18:08:12 +00:00
|
|
|
static QIcon getIconFor(const QString& str, int pos)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
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);
|
|
|
|
}
|
2017-09-28 20:10:02 +00:00
|
|
|
|
|
|
|
static QString formatBytecount(const long bytecount)
|
|
|
|
{
|
|
|
|
const int exp = log(bytecount) / log(1000);
|
|
|
|
constexpr char suffixes[] = {' ', 'k', 'M', 'G', 'T', 'P', 'E'};
|
|
|
|
|
|
|
|
QString str;
|
|
|
|
QTextStream stream(&str);
|
|
|
|
stream << qSetRealNumberPrecision(3) << bytecount / pow(1000, exp)
|
|
|
|
<< ' ' << suffixes[exp] << 'B';
|
|
|
|
return stream.readAll();
|
|
|
|
}
|
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
NewFileDialog::NewFileDialog(QWidget *parent) :
|
2017-11-03 17:22:54 +00:00
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::NewFileDialog)
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2017-03-31 00:51:14 +00:00
|
|
|
setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
|
2017-12-06 16:25:07 +00:00
|
|
|
setAcceptDrops(true);
|
2017-10-21 19:20:10 +00:00
|
|
|
ui->recentsListWidget->addAction(ui->actionRemove_item);
|
|
|
|
ui->recentsListWidget->addAction(ui->actionClear_all);
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-12-06 15:52:59 +00:00
|
|
|
QString logoFile = (palette().window().color().value() < 127) ? ":/img/cutter_white_plain.svg" : ":/img/cutter_plain.svg";
|
2017-12-03 16:26:01 +00:00
|
|
|
ui->logoSvgWidget->load(logoFile);
|
2017-12-03 14:46:22 +00:00
|
|
|
|
2017-10-21 19:20:10 +00:00
|
|
|
fillRecentFilesList();
|
|
|
|
bool projectsExist = fillProjectsList();
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-10-21 19:20:10 +00:00
|
|
|
if(projectsExist)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-10-21 19:20:10 +00:00
|
|
|
ui->tabWidget->setCurrentWidget(ui->projectsTab);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->tabWidget->setCurrentWidget(ui->filesTab);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hide "create" button until the dialog works
|
|
|
|
ui->createButton->hide();
|
2017-09-29 07:59:03 +00:00
|
|
|
|
2017-11-03 17:22:54 +00:00
|
|
|
ui->loadProjectButton->setEnabled(ui->projectsListWidget->currentItem() != nullptr);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 09:41:28 +00:00
|
|
|
NewFileDialog::~NewFileDialog() {}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
|
|
|
void NewFileDialog::on_loadFileButton_clicked()
|
|
|
|
{
|
2017-10-21 19:20:10 +00:00
|
|
|
loadFile(ui->newFileEdit->text());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::on_selectFileButton_clicked()
|
|
|
|
{
|
|
|
|
QString fileName = QFileDialog::getOpenFileName(this, tr("Select file"), QDir::homePath());
|
|
|
|
|
|
|
|
if (!fileName.isEmpty())
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-10-21 19:20:10 +00:00
|
|
|
ui->newFileEdit->setText(fileName);
|
|
|
|
ui->loadFileButton->setFocus();
|
2017-04-09 19:55:06 +00:00
|
|
|
}
|
2017-10-21 19:20:10 +00:00
|
|
|
}
|
2017-04-04 21:00:55 +00:00
|
|
|
|
2017-10-21 19:20:10 +00:00
|
|
|
void NewFileDialog::on_selectProjectsDirButton_clicked()
|
|
|
|
{
|
2017-11-03 17:22:54 +00:00
|
|
|
QFileDialog dialog(this);
|
|
|
|
dialog.setFileMode(QFileDialog::DirectoryOnly);
|
2017-04-09 19:55:06 +00:00
|
|
|
|
2017-10-21 19:20:10 +00:00
|
|
|
QString currentDir = CutterCore::getInstance()->getConfig("dir.projects");
|
|
|
|
if(currentDir.startsWith("~"))
|
|
|
|
{
|
|
|
|
currentDir = QDir::homePath() + currentDir.mid(1);
|
|
|
|
}
|
2017-11-03 17:22:54 +00:00
|
|
|
dialog.setDirectory(currentDir);
|
2017-05-13 18:09:36 +00:00
|
|
|
|
2017-10-21 19:20:10 +00:00
|
|
|
dialog.setWindowTitle(tr("Select project path (dir.projects)"));
|
|
|
|
|
2017-11-03 17:22:54 +00:00
|
|
|
if(!dialog.exec())
|
2017-10-21 19:20:10 +00:00
|
|
|
{
|
|
|
|
return;
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
2017-10-21 19:20:10 +00:00
|
|
|
|
2017-11-03 17:22:54 +00:00
|
|
|
QString dir = dialog.selectedFiles().first();
|
|
|
|
if (!dir.isEmpty())
|
|
|
|
{
|
|
|
|
CutterCore::getInstance()->setConfig("dir.projects", dir);
|
|
|
|
fillProjectsList();
|
|
|
|
}
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 19:20:10 +00:00
|
|
|
void NewFileDialog::on_loadProjectButton_clicked()
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2017-11-03 17:22:54 +00:00
|
|
|
QListWidgetItem *item = ui->projectsListWidget->currentItem();
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-11-03 17:22:54 +00:00
|
|
|
if (item == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-11-03 17:22:54 +00:00
|
|
|
loadProject(item->data(Qt::UserRole).toString());
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 19:20:10 +00:00
|
|
|
void NewFileDialog::on_recentsListWidget_itemClicked(QListWidgetItem *item)
|
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();
|
|
|
|
ui->newFileEdit->setText(sitem);
|
|
|
|
}
|
|
|
|
|
2017-10-21 19:20:10 +00:00
|
|
|
void NewFileDialog::on_recentsListWidget_itemDoubleClicked(QListWidgetItem *item)
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2017-10-21 19:20:10 +00:00
|
|
|
loadFile(item->data(Qt::UserRole).toString());
|
|
|
|
}
|
2017-05-13 18:09:36 +00:00
|
|
|
|
2017-10-21 19:20:10 +00:00
|
|
|
void NewFileDialog::on_projectsListWidget_itemSelectionChanged()
|
|
|
|
{
|
2017-11-03 17:22:54 +00:00
|
|
|
ui->loadProjectButton->setEnabled(ui->projectsListWidget->currentItem() != nullptr);
|
2017-10-21 19:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::on_projectsListWidget_itemDoubleClicked(QListWidgetItem *item)
|
|
|
|
{
|
|
|
|
loadProject(item->data(Qt::UserRole).toString());
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::on_cancelButton_clicked()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2017-12-04 21:38:09 +00:00
|
|
|
void NewFileDialog::on_aboutButton_clicked()
|
|
|
|
{
|
|
|
|
AboutDialog *a = new AboutDialog(this);
|
|
|
|
a->open();
|
|
|
|
}
|
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
void NewFileDialog::on_actionRemove_item_triggered()
|
|
|
|
{
|
|
|
|
// Remove selected item from recents list
|
2017-10-21 19:20:10 +00:00
|
|
|
QListWidgetItem *item = ui->recentsListWidget->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-10-21 19:20:10 +00:00
|
|
|
ui->recentsListWidget->takeItem(ui->recentsListWidget->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-10-02 09:41:28 +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();
|
|
|
|
|
2017-10-21 19:20:10 +00:00
|
|
|
ui->recentsListWidget->clear();
|
2017-03-29 10:18:37 +00:00
|
|
|
// TODO: if called from main window its ok, otherwise its not
|
|
|
|
settings.setValue("recentFileList", files);
|
|
|
|
ui->newFileEdit->clear();
|
|
|
|
}
|
2017-10-21 19:20:10 +00:00
|
|
|
|
2017-12-06 16:25:07 +00:00
|
|
|
void NewFileDialog::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
|
|
|
// Accept drag & drop events only if they provide a URL
|
|
|
|
if(event->mimeData()->hasUrls())
|
|
|
|
{
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::dropEvent(QDropEvent *event)
|
|
|
|
{
|
|
|
|
// Accept drag & drop events only if they provide a URL
|
2017-12-06 16:34:35 +00:00
|
|
|
if(event->mimeData()->urls().count() == 0)
|
|
|
|
{
|
2017-12-06 16:25:07 +00:00
|
|
|
qWarning() << "No URL in drop event, ignoring it.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->acceptProposedAction();
|
|
|
|
loadFile(event->mimeData()->urls().first().path());
|
|
|
|
}
|
|
|
|
|
2017-10-21 19:20:10 +00:00
|
|
|
bool NewFileDialog::fillRecentFilesList()
|
|
|
|
{
|
|
|
|
// Fill list with recent opened files
|
|
|
|
QSettings settings;
|
|
|
|
|
|
|
|
QStringList files = settings.value("recentFileList").toStringList();
|
|
|
|
|
|
|
|
QMutableListIterator<QString> it(files);
|
|
|
|
int i = 0;
|
|
|
|
while (it.hasNext())
|
|
|
|
{
|
|
|
|
const QString &file = it.next();
|
|
|
|
// Get stored files
|
|
|
|
|
|
|
|
// Remove all but the file name
|
|
|
|
const QString sep = QDir::separator();
|
|
|
|
const QStringList name_list = file.split(sep);
|
|
|
|
const QString name = name_list.last();
|
|
|
|
|
|
|
|
// Get file info
|
|
|
|
QFileInfo info(file);
|
|
|
|
if (!info.exists())
|
|
|
|
{
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QListWidgetItem *item = new QListWidgetItem(
|
|
|
|
getIconFor(name, i++),
|
|
|
|
file + "\nCreated: " + info.created().toString() + "\nSize: " + formatBytecount(info.size())
|
|
|
|
);
|
|
|
|
//":/img/icons/target.svg"), name );
|
|
|
|
item->setData(Qt::UserRole, file);
|
|
|
|
ui->recentsListWidget->addItem(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removed files were deleted from the stringlist. Save it again.
|
|
|
|
settings.setValue("recentFileList", files);
|
|
|
|
|
|
|
|
return !files.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NewFileDialog::fillProjectsList()
|
|
|
|
{
|
|
|
|
CutterCore *core = CutterCore::getInstance();
|
|
|
|
|
2017-11-03 17:22:54 +00:00
|
|
|
ui->projectsDirEdit->setText(core->getConfig("dir.projects"));
|
2017-10-21 19:20:10 +00:00
|
|
|
|
|
|
|
QStringList projects = core->getProjectNames();
|
|
|
|
projects.sort(Qt::CaseInsensitive);
|
|
|
|
|
2017-11-03 17:22:54 +00:00
|
|
|
ui->projectsListWidget->clear();
|
2017-10-21 19:20:10 +00:00
|
|
|
|
|
|
|
int i=0;
|
|
|
|
for(const QString &project : projects)
|
|
|
|
{
|
|
|
|
QString info = core->cmd("Pi " + project);
|
|
|
|
|
|
|
|
QListWidgetItem *item = new QListWidgetItem(getIconFor(project, i++), project + "\n" + info);
|
|
|
|
|
|
|
|
item->setData(Qt::UserRole, project);
|
|
|
|
ui->projectsListWidget->addItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !projects.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::loadFile(const QString &filename)
|
|
|
|
{
|
|
|
|
// Check that there is a file selected
|
|
|
|
QFileInfo checkfile(filename);
|
|
|
|
if (!checkfile.exists() || !checkfile.isFile())
|
|
|
|
{
|
|
|
|
QMessageBox msgBox(this);
|
2017-12-04 21:38:09 +00:00
|
|
|
msgBox.setText(tr("Select a new program or a previous one\nbefore continuing"));
|
2017-10-21 19:20:10 +00:00
|
|
|
msgBox.exec();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Add file to recent file list
|
|
|
|
QSettings settings;
|
|
|
|
QStringList files = settings.value("recentFileList").toStringList();
|
|
|
|
files.removeAll(filename);
|
|
|
|
files.prepend(filename);
|
|
|
|
while (files.size() > MaxRecentFiles)
|
|
|
|
files.removeLast();
|
|
|
|
|
|
|
|
settings.setValue("recentFileList", files);
|
|
|
|
|
|
|
|
// Close dialog and open MainWindow/OptionsDialog
|
|
|
|
MainWindow *main = new MainWindow();
|
|
|
|
main->openNewFile(filename);
|
2017-11-26 12:36:24 +00:00
|
|
|
|
|
|
|
close();
|
2017-10-21 19:20:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewFileDialog::loadProject(const QString &project)
|
|
|
|
{
|
|
|
|
MainWindow *main = new MainWindow();
|
|
|
|
main->openProject(project);
|
2017-11-26 13:17:16 +00:00
|
|
|
|
2017-11-26 12:36:24 +00:00
|
|
|
close();
|
2017-10-21 19:20:10 +00:00
|
|
|
}
|