Commits

zjes committed 38500eb

Add create project

Comments (0)

Files changed (13)

src/Core/FileNameSelect.cpp

+#include <QDebug>
 #include <QHBoxLayout>
 #include <QFileSystemModel>
 #include <QCompleter>
 
 FileNameSelect::FileNameSelect(QWidget* parent):
     QWidget(parent),
-    _caption(tr("Select file"))
+    _caption(tr("Select file")),
+    _dirOnly(false),
+    _defaultPath(QDir::homePath())
 {
     setLayout(new QHBoxLayout);
     _txt = new QLineEdit(this);
 void FileNameSelect::setText(const QString& text)
 {
     _txt->setText(text);
+    qobject_cast<QFileSystemModel *>(_txt->completer()->model())->setRootPath(text);
     emit textChanged();
 }
 
 void FileNameSelect::onSelectClicked()
 {
     QFileInfo info(_txt->text());
-    QString file = QFileDialog::getOpenFileName(this, _caption, info.absoluteDir().path());
-    if (!file.isEmpty())
-        _txt->setText(file);
+    if (!_dirOnly){
+        QString file = QFileDialog::getOpenFileName(this, _caption, _txt->text().isEmpty() ? _defaultPath : info.absoluteDir().path());
+        if (!file.isEmpty())
+            _txt->setText(file);
+    } else {
+        QFileDialog dlg(this);
+        dlg.setFileMode(QFileDialog::DirectoryOnly);
+        dlg.setWindowTitle(_caption);
+        qDebug() << info.absolutePath();
+        dlg.setDirectory(info.absoluteFilePath());
+        if (dlg.exec() == QDialog::Accepted)
+            _txt->setText(dlg.directory().absolutePath());
+    }
+}
+
+void FileNameSelect::setDirOnly(bool dirOnly)
+{
+    _dirOnly = dirOnly;
+    _caption = _dirOnly ? tr("Select directory") : tr("Select file");
+}
+
+void FileNameSelect::setDefaultPath(const QString& path)
+{
+    _defaultPath = path;
 }

src/Core/FileNameSelect.h

 public slots:
     void setText(const QString& text);
     void setCaption(const QString& text);
+    void setDirOnly(bool dirOnly);
+    void setDefaultPath(const QString& path);
 public:
     QString text();
     QString caption();
     QLineEdit * _txt;
     QPushButton * _btn;
     QString _caption;
+    bool _dirOnly;
+    QString _defaultPath;
 };
 
 #endif

src/Main/PynotoWindow.cpp

 
 void PynotoWindow::newProject()
 {
-
+    _project->createNewProject(this);
 }
 
 void PynotoWindow::resetEditor()

src/Project/CMakeLists.txt

     Properties/ProjectProperties.h
     Properties/General.h
     Properties/CodeCheck.h
+    CreateProjectDlg.h
 )
 
 SET(moc
     Properties/ProjectProperties.cpp
     Properties/General.cpp
     Properties/CodeCheck.cpp
+    CreateProjectDlg.cpp
 )
 
 SET(res
     Properties/ProjectProperties.ui
     Properties/General.ui
     Properties/CodeCheck.ui
+    CreateProjectDlg.ui
 )
 
 QT4_ADD_RESOURCES(qrc ${res})

src/Project/CreateProjectDlg.cpp

+#include <QDir>
+#include "CreateProjectDlg.h"
+#include "ui_CreateProjectDlg.h"
+
+namespace Project {
+
+CreateProjectDlg::CreateProjectDlg(QWidget *parent):
+    QDialog(parent),
+    _ui(new Ui::CreateProjectDlg)
+{
+    _ui->setupUi(this);
+    _ui->projectPath->setText(QDir::homePath());
+    _ui->projectPath->setDirOnly(true);
+    _ui->projectName->setValidator(new QRegExpValidator(QRegExp("[a-zA-Z0-9 \\-_]+"), this));
+
+    _ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
+    connect(_ui->projectName, SIGNAL(textChanged(QString)), SLOT(onNameChanged(QString)));
+}
+
+CreateProjectDlg::~CreateProjectDlg()
+{
+    delete _ui;
+}
+
+void CreateProjectDlg::onNameChanged(const QString& text)
+{
+    int pos = 0;
+    QString txt(text);
+    _ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(
+        _ui->projectName->validator()->validate(txt, pos) == QValidator::Acceptable
+    );
+}
+
+QString CreateProjectDlg::projectPath()
+{
+    return QDir(_ui->projectPath->text()).filePath(_ui->projectName->text()+".pynotoprj");
+}
+
+QString CreateProjectDlg::name()
+{
+    return _ui->projectName->text().trimmed();
+}
+
+}

src/Project/CreateProjectDlg.h

+#ifndef _CREATEPROJECTDLG_H_
+#define _CREATEPROJECTDLG_H_
+
+#include <QDialog>
+
+namespace Ui { class CreateProjectDlg; }
+namespace Project {
+
+class CreateProjectDlg: public QDialog
+{
+    Q_OBJECT
+public:
+    CreateProjectDlg(QWidget *parent);
+    virtual ~CreateProjectDlg();
+
+    QString projectPath();
+    QString name();
+private slots:
+    void onNameChanged(const QString& text);
+private:
+    Ui::CreateProjectDlg *_ui;
+};
+
+}
+#endif

src/Project/CreateProjectDlg.ui

+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>CreateProjectDlg</class>
+ <widget class="QDialog" name="CreateProjectDlg">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>398</width>
+    <height>162</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Create new project</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <layout class="QFormLayout" name="formLayout">
+     <item row="0" column="0">
+      <widget class="QLabel" name="label">
+       <property name="text">
+        <string>Project name:</string>
+       </property>
+      </widget>
+     </item>
+     <item row="1" column="0">
+      <widget class="QLabel" name="label_2">
+       <property name="text">
+        <string>Project directory:</string>
+       </property>
+      </widget>
+     </item>
+     <item row="0" column="1">
+      <widget class="QLineEdit" name="projectName"/>
+     </item>
+     <item row="1" column="1">
+      <widget class="FileNameSelect" name="projectPath" native="true">
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
+         <horstretch>0</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </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>
+ <customwidgets>
+  <customwidget>
+   <class>FileNameSelect</class>
+   <extends>QWidget</extends>
+   <header>Core/FileNameSelect.h</header>
+   <container>1</container>
+  </customwidget>
+ </customwidgets>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>CreateProjectDlg</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>316</x>
+     <y>260</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>CreateProjectDlg</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>248</x>
+     <y>254</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>

src/Project/IProjectPlugin.h

     void preferecesChanged();
 public slots:
     virtual void editProperties(QWidget * parent) = 0;
+    virtual void createNewProject(QWidget *parent) = 0;
 };
 
 }

src/Project/ProjectPlugin.cpp

 #include <QDebug>
 #include <QtPlugin>
+#include <QFileDialog>
+#include <QMessageBox>
 #include "ProjectPlugin.h"
 #include "ProjectManagerWidget.h"
 #include "Properties/ProjectProperties.h"
+#include "CreateProjectDlg.h"
 
 namespace Project {
 
     pprop.exec();
 }
 
+void ProjectPlugin::createNewProject(QWidget *parent)
+{
+    CreateProjectDlg dlg(parent);
+    if (dlg.exec() == QDialog::Accepted){
+        QString path = dlg.projectPath();
+        if (QFile::exists(path)){
+            if (QMessageBox::question(
+                        parent,
+                        tr("File exists"),
+                        tr("Project file %1 already exists. Owerride?").arg(path),
+                        QMessageBox::Yes | QMessageBox::No
+                        ) == QMessageBox::No)
+                return;
+        }
+        QFile file(path);
+        if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)){
+            QTextStream st(&file);
+            st << "[common]" << endl;
+            st << "name=" << dlg.name() << endl;
+            file.close();
+        }
+        _project->unload();
+        _project->load(path);
+        ProjectProperties pprop(_project, parent);
+        pprop.exec();
+    }
+}
+
 }
 
 Q_EXPORT_PLUGIN2(project, Project::ProjectPlugin)

src/Project/ProjectPlugin.h

     virtual IProject * project();
 public slots:
     virtual void editProperties(QWidget * parent);
+    virtual void createNewProject(QWidget *parent);
 private:
     ProjectFile * _project;
 };

src/Project/Properties/CodeCheck.ui

    <rect>
     <x>0</x>
     <y>0</y>
-    <width>512</width>
-    <height>486</height>
+    <width>510</width>
+    <height>484</height>
    </rect>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QTabWidget" name="tabWidget">
      <property name="currentIndex">
-      <number>1</number>
+      <number>0</number>
      </property>
      <widget class="QWidget" name="tab">
       <attribute name="title">

src/Project/Properties/General.cpp

     _project = project;
 
     _ui->edtProjectName->setText(_project->name());
-    _ui->edtMainScript->setText(_project->mainScript());
+    _ui->mainScript->setText(_project->mainScript());
+    _ui->mainScript->setDefaultPath(_project->path());
     _ui->edtFileToShow->setText(_project->shownFiles().join(";"));
 
     refreshPyVersions();
 bool General::save()
 {
     _project->setName(_ui->edtProjectName->text());
-    _project->setMainScript(_ui->edtMainScript->text());
+    _project->setMainScript(_ui->mainScript->text());
     _project->setShownFiles(_ui->edtFileToShow->text().split(";"));
     _project->setInterpretName(_ui->cmbPyVersion->itemData(_ui->cmbPyVersion->currentIndex()).toString());
     _project->setGuiName(_ui->cmdFrameworks->itemData(_ui->cmdFrameworks->currentIndex()).toString());

src/Project/Properties/General.ui

    <rect>
     <x>0</x>
     <y>0</y>
-    <width>535</width>
-    <height>428</height>
+    <width>533</width>
+    <height>426</height>
    </rect>
   </property>
   <property name="windowTitle">
        </property>
       </widget>
      </item>
-     <item row="1" column="1">
-      <layout class="QHBoxLayout" name="horizontalLayout_2">
-       <item>
-        <widget class="QLineEdit" name="edtMainScript"/>
-       </item>
-       <item>
-        <widget class="QPushButton" name="btnSelectMain">
-         <property name="text">
-          <string>Select</string>
-         </property>
-        </widget>
-       </item>
-      </layout>
-     </item>
      <item row="4" column="0">
       <widget class="QLabel" name="lbl">
        <property name="text">
      <item row="4" column="1">
       <widget class="QLineEdit" name="edtFileToShow"/>
      </item>
+     <item row="1" column="1">
+      <widget class="FileNameSelect" name="mainScript" native="true">
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
+         <horstretch>0</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+      </widget>
+     </item>
     </layout>
    </item>
   </layout>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>FileNameSelect</class>
+   <extends>QWidget</extends>
+   <header>Core/FileNameSelect.h</header>
+   <container>1</container>
+  </customwidget>
+ </customwidgets>
  <resources/>
  <connections/>
 </ui>