Fri, 02 Aug 2013 18:42:06 +0300
added missing files
src/download.cpp | file | annotate | diff | comparison | revisions | |
src/download.h | file | annotate | diff | comparison | revisions | |
src/ui/downloadfrom.ui | file | annotate | diff | comparison | revisions |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/download.cpp Fri Aug 02 18:42:06 2013 +0300 @@ -0,0 +1,162 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 Santeri Piippo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "download.h" +#include "ui_downloadfrom.h" +#include "types.h" +#include "gui.h" +#include "build/moc_download.cpp" + +PartDownloader g_PartDownloader; + +constexpr const char* PartDownloader::k_OfficialURL, + *PartDownloader::k_UnofficialURL; + +// ============================================================================= +// ----------------------------------------------------------------------------- +void PartDownloader::download() { + PartDownloadPrompt* dlg = new PartDownloadPrompt; + dlg->exec(); +} + +// ============================================================================= +// ----------------------------------------------------------------------------- +PartDownloadPrompt::PartDownloadPrompt (QWidget* parent) : QDialog (parent) { + ui = new Ui_DownloadFrom; + ui->setupUi (this); + connect (ui->source, SIGNAL (currentIndexChanged (int)), this, SLOT (sourceChanged (int))); + connect (ui->buttonBox, SIGNAL (accepted()), this, SLOT (startDownload())); +} + +// ============================================================================= +// ----------------------------------------------------------------------------- +PartDownloadPrompt::~PartDownloadPrompt() { + delete ui; +} + +// ============================================================================= +// ----------------------------------------------------------------------------- +str PartDownloadPrompt::getURL() { + str fname; + Source src = (Source) ui->source->currentIndex(); + + switch (src) { + case OfficialLibrary: + case PartsTracker: + { + fname = ui->fname->text(); + + // Ensure .dat extension + if (fname.right (4) != ".dat") { + // Remove the existing extension, if any. It may be we're here over a + // typo in the .dat extension. + if (fname.lastIndexOf (".") >= fname.length() - 4) + fname.chop (fname.length() - fname.lastIndexOf (".")); + + fname += ".dat"; + } + + /* These sources have stuff only in parts/, parts/s/, p/, and p/48/. If + * we haven't already specified either parts/ or p/, we need to add it + * automatically. Part files are numbers which can be followed by: + * - c** (composites) + * - d** (formed stickers) + * - a lowercase alphabetic letter for variants + * + * Subfiles have an s** prefix, in which case we use parts/s/. Note that + * the regex starts with a '^' so it won't catch already fully given part + * file names. + */ + str partRegex = "^[0-9]+(c[0-9][0-9]+)*(d[0-9][0-9]+)*[a-z]?"; + str subpartRegex = partRegex + "s[0-9][0-9]+"; + + partRegex += "\\.dat$"; + subpartRegex += "\\.dat$"; + + if (QRegExp (subpartRegex).exactMatch (fname)) + fname.prepend ("parts/s/"); + elif (QRegExp (partRegex).exactMatch (fname)) + fname.prepend ("parts/"); + elif (fname.left (6) != "parts/" && fname.left (2) != "p/") + fname.prepend ("p/"); + } + + if (src == OfficialLibrary) + return str (PartDownloader::k_OfficialURL) + fname; + + return str (PartDownloader::k_UnofficialURL) + fname; + + case CustomURL: + return ui->fname->text(); + } + + // Shouldn't happen + return ""; +} + +// ============================================================================= +// ----------------------------------------------------------------------------- +void PartDownloadPrompt::sourceChanged (int i) { + if (i == CustomURL) + ui->fileNameLabel->setText (tr ("URL:")); + else + ui->fileNameLabel->setText (tr ("File name:")); +} + +void PartDownloadPrompt::startDownload() { + int row = ui->progress->rowCount(); + ui->progress->setEnabled (true); + ui->buttonBox->setEnabled (false); + ui->progress->insertRow (row); + + PartDownloadRequest* req = new PartDownloadRequest (getURL(), this); + req->setTableRow (row); + req->updateToTable(); +} + +// ============================================================================= +// ----------------------------------------------------------------------------- +PartDownloadRequest::PartDownloadRequest (str url, PartDownloadPrompt* parent) : + QObject (parent), + m_prompt (parent), + m_url (url) {} + +// ============================================================================= +// ----------------------------------------------------------------------------- +void PartDownloadRequest::updateToTable() { + QTableWidget* table = m_prompt->ui->progress; + QTableWidgetItem* urlItem = table->item (tableRow(), 0), + *progressItem = table->item (tableRow(), 1); + + if (!urlItem || !progressItem) { + urlItem = new QTableWidgetItem; + progressItem = new QTableWidgetItem; + + table->setItem (tableRow(), 0, urlItem); + table->setItem (tableRow(), 1, progressItem); + } + + urlItem->setText (m_url); + progressItem->setText ("---"); +} + +// ============================================================================= +// ----------------------------------------------------------------------------- +DEFINE_ACTION (DownloadFrom, 0) { + g_PartDownloader.download(); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/download.h Fri Aug 02 18:42:06 2013 +0300 @@ -0,0 +1,83 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 Santeri Piippo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef LDFORGE_DOWNLOAD_H +#define LDFORGE_DOWNLOAD_H + +#include <QDialog> +#include "common.h" + +class Ui_DownloadFrom; +class QNetworkAccessManager; +class QNetworkRequest; +class QNetworkReply; + +// ============================================================================= +// ----------------------------------------------------------------------------- +extern class PartDownloader { +public: + constexpr static const char* k_OfficialURL = "http://ldraw.org/library/official/", + *k_UnofficialURL = "http://ldraw.org/library/unofficial/"; + + PartDownloader() {} + void download(); + void operator()() { download(); } +} g_PartDownloader; + +// ============================================================================= +// ----------------------------------------------------------------------------- +class PartDownloadPrompt : public QDialog { + Q_OBJECT + +public: + enum Source { + OfficialLibrary, + PartsTracker, + CustomURL, + }; + + explicit PartDownloadPrompt (QWidget* parent = null); + virtual ~PartDownloadPrompt(); + str getURL(); + +public slots: + void sourceChanged (int i); + void startDownload(); + +protected: + Ui_DownloadFrom* ui; + friend class PartDownloadRequest; +}; + +// ============================================================================= +// ----------------------------------------------------------------------------- +class PartDownloadRequest : public QObject { + PROPERTY (int, tableRow, setTableRow) + +public: + explicit PartDownloadRequest (str url, PartDownloadPrompt* parent); + void updateToTable(); + +private: + PartDownloadPrompt* m_prompt; + str m_url; + QNetworkAccessManager* m_nam; + QNetworkReply* m_reply; +}; + +#endif // LDFORGE_DOWNLOAD_H \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ui/downloadfrom.ui Fri Aug 02 18:42:06 2013 +0300 @@ -0,0 +1,161 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>DownloadFrom</class> + <widget class="QDialog" name="DownloadFrom"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>406</width> + <height>319</height> + </rect> + </property> + <property name="windowTitle"> + <string>Download from LDraw.org</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QLabel" name="label"> + <property name="font"> + <font> + <pointsize>11</pointsize> + <weight>75</weight> + <bold>true</bold> + </font> + </property> + <property name="text"> + <string>Download from LDraw.org</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item> + <layout class="QFormLayout" name="formLayout"> + <property name="fieldGrowthPolicy"> + <enum>QFormLayout::ExpandingFieldsGrow</enum> + </property> + <item row="0" column="0"> + <widget class="QLabel" name="label_2"> + <property name="text"> + <string>Source:</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QComboBox" name="source"> + <property name="sizePolicy"> + <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <item> + <property name="text"> + <string>Official parts</string> + </property> + </item> + <item> + <property name="text"> + <string>Parts tracker</string> + </property> + </item> + <item> + <property name="text"> + <string>Custom URL</string> + </property> + </item> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="fileNameLabel"> + <property name="text"> + <string>File name:</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLineEdit" name="fname"/> + </item> + </layout> + </item> + <item> + <widget class="QTableWidget" name="progress"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="editTriggers"> + <set>QAbstractItemView::NoEditTriggers</set> + </property> + <property name="cornerButtonEnabled"> + <bool>false</bool> + </property> + <attribute name="horizontalHeaderCascadingSectionResizes"> + <bool>false</bool> + </attribute> + <attribute name="horizontalHeaderShowSortIndicator" stdset="0"> + <bool>true</bool> + </attribute> + <attribute name="horizontalHeaderStretchLastSection"> + <bool>true</bool> + </attribute> + <attribute name="verticalHeaderVisible"> + <bool>false</bool> + </attribute> + <attribute name="verticalHeaderCascadingSectionResizes"> + <bool>false</bool> + </attribute> + <attribute name="verticalHeaderStretchLastSection"> + <bool>false</bool> + </attribute> + <column> + <property name="text"> + <string>URL</string> + </property> + <property name="font"> + <font> + <weight>50</weight> + <bold>false</bold> + </font> + </property> + </column> + <column> + <property name="text"> + <string>Status</string> + </property> + </column> + </widget> + </item> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="standardButtons"> + <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections> + <connection> + <sender>buttonBox</sender> + <signal>rejected()</signal> + <receiver>DownloadFrom</receiver> + <slot>reject()</slot> + <hints> + <hint type="sourcelabel"> + <x>322</x> + <y>312</y> + </hint> + <hint type="destinationlabel"> + <x>286</x> + <y>274</y> + </hint> + </hints> + </connection> + </connections> +</ui>