Skip to content

Commit

Permalink
Add UI and logic for collectively adding repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
soramimi committed Jun 6, 2024
1 parent e2b65eb commit 959aad9
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 14 deletions.
9 changes: 9 additions & 0 deletions Guitar.pro
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ CONFIG(debug,debug|release):TARGET = Guitard
CONFIG(release,debug|release):TARGET = Guitar
DESTDIR=$$PWD/_bin

FORMS += \
src/AddRepositoriesCollectivelyDialog.ui

HEADERS += \
src/AddRepositoriesCollectivelyDialog.h

SOURCES += \
src/AddRepositoriesCollectivelyDialog.cpp

35 changes: 35 additions & 0 deletions src/AddRepositoriesCollectivelyDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "AddRepositoriesCollectivelyDialog.h"
#include "ui_AddRepositoriesCollectivelyDialog.h"

AddRepositoriesCollectivelyDialog::AddRepositoriesCollectivelyDialog(QWidget *parent, QStringList const &dirs)
: QDialog(parent)
, ui(new Ui::AddRepositoriesCollectivelyDialog)
{
ui->setupUi(this);

QStringList list = dirs;
std::sort(list.begin(), list.end());

for (QString const &dir : list) {
QListWidgetItem *item = new QListWidgetItem(dir);
item->setCheckState(Qt::Checked);
ui->listWidget->addItem(item);
}
}

AddRepositoriesCollectivelyDialog::~AddRepositoriesCollectivelyDialog()
{
delete ui;
}

QStringList AddRepositoriesCollectivelyDialog::selectedDirs() const
{
QStringList dirs;
for (int i = 0; i < ui->listWidget->count(); i++) {
QListWidgetItem *item = ui->listWidget->item(i);
if (item->checkState() == Qt::Checked) {
dirs.append(item->text());
}
}
return dirs;
}
22 changes: 22 additions & 0 deletions src/AddRepositoriesCollectivelyDialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef ADDREPOSITORIESCOLLECTIVELYDIALOG_H
#define ADDREPOSITORIESCOLLECTIVELYDIALOG_H

#include <QDialog>

namespace Ui {
class AddRepositoriesCollectivelyDialog;
}

class AddRepositoriesCollectivelyDialog : public QDialog {
Q_OBJECT
private:
Ui::AddRepositoriesCollectivelyDialog *ui;

public:
explicit AddRepositoriesCollectivelyDialog(QWidget *parent, const QStringList &dirs);
~AddRepositoriesCollectivelyDialog();

QStringList selectedDirs() const;
};

#endif // ADDREPOSITORIESCOLLECTIVELYDIALOG_H
91 changes: 91 additions & 0 deletions src/AddRepositoriesCollectivelyDialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AddRepositoriesCollectivelyDialog</class>
<widget class="QDialog" name="AddRepositoriesCollectivelyDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Add Repositories</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="listWidget"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>&amp;OK</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>&amp;Cancel</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>AddRepositoriesCollectivelyDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>233</x>
<y>279</y>
</hint>
<hint type="destinationlabel">
<x>213</x>
<y>279</y>
</hint>
</hints>
</connection>
<connection>
<sender>pushButton_2</sender>
<signal>clicked()</signal>
<receiver>AddRepositoriesCollectivelyDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>326</x>
<y>289</y>
</hint>
<hint type="destinationlabel">
<x>308</x>
<y>295</y>
</hint>
</hints>
</connection>
</connections>
</ui>
42 changes: 28 additions & 14 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "UserEvent.h"
#include "WelcomeWizardDialog.h"
#include "common/misc.h"
#include "AddRepositoriesCollectivelyDialog.h"
#include "gunzip.h"
#include "platform.h"
#include "webclient.h"
Expand Down Expand Up @@ -1203,22 +1204,15 @@ bool MainWindow::addExistingLocalRepository(QString dir, QString name, QString s
*/
void MainWindow::addExistingLocalRepositoryWithGroup(const QString &dir, const QString &group)
{
std::set<QString> existing;
for (RepositoryData const &item : m->repos) {
existing.insert(item.local_dir);
}

QFileInfo info1(dir);
if (info1.isDir()) {
QFileInfo info2(dir / ".git");
if (info2.isDir()) {
RepositoryData item;
item.local_dir = info1.absoluteFilePath();
if (existing.find(item.local_dir) == existing.end()) { // 重複していなければ
item.name = makeRepositoryName(item.local_dir);
item.group = group;
m->repos.append(item); // 追加
}
item.name = makeRepositoryName(item.local_dir);
item.group = group;
m->repos.append(item);
}
}
}
Expand Down Expand Up @@ -2331,14 +2325,34 @@ void MainWindow::scanFolderAndRegister(QString const &group)
{
QString path = QFileDialog::getExistingDirectory(this, tr("Select a folder"), QString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!path.isEmpty()) {
QStringList dirs;
std::set<QString> existing;
for (RepositoryData const &item : m->repos) {
existing.insert(item.local_dir);
}
QDirIterator it(path, QDir::Dirs | QDir::NoDotAndDotDot);
while (it.hasNext()) {
it.next();
QFileInfo info = it.fileInfo();
QString local_dir = info.absoluteFilePath();
addExistingLocalRepositoryWithGroup(local_dir, group);
if (existing.find(local_dir) == existing.end()) {
if (Git::isValidWorkingCopy(local_dir)) {
dirs.push_back(local_dir);
}
}
}
if (dirs.isEmpty()) {
QMessageBox::warning(this, tr("No repositories found"), tr("No repositories found in the folder."));
} else {
AddRepositoriesCollectivelyDialog dlg(this, dirs);
if (dlg.exec() == QDialog::Accepted) {
dirs = dlg.selectedDirs();
for (QString const &dir : dirs) {
addExistingLocalRepositoryWithGroup(dir, group);
}
saveRepositoryBookmarks(true);
}
}
saveRepositoryBookmarks(true);
}
}

Expand Down Expand Up @@ -4629,7 +4643,7 @@ void MainWindow::on_treeWidget_repos_customContextMenuRequested(const QPoint &po
QAction *a_rename_group = menu.addAction(tr("&Rename group"));
menu.addSeparator();
QAction *a_add_repository = menu.addAction(tr("&Add repository"));
QAction *a_scan_folder_and_register = menu.addAction(tr("&Scan folder and register"));
QAction *a_scan_folder_and_add = menu.addAction(tr("&Scan folder and add"));
QPoint pt = ui->treeWidget_repos->mapToGlobal(pos);
QAction *a = menu.exec(pt + QPoint(8, -8));
if (a) {
Expand Down Expand Up @@ -4661,7 +4675,7 @@ void MainWindow::on_treeWidget_repos_customContextMenuRequested(const QPoint &po
addRepository({}, group);
return;
}
if (a == a_scan_folder_and_register) {
if (a == a_scan_folder_and_add) {
QString group = treeItemGroup(treeitem) / treeItemName(treeitem);
scanFolderAndRegister(group);
return;
Expand Down

0 comments on commit 959aad9

Please sign in to comment.