File indexing completed on 2024-06-16 08:27:24

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2005 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "newfiledlg.h"
0012 #include "config.h"
0013 #include "document.h"
0014 #include "microinfo.h"
0015 #include "microlibrary.h"
0016 #include "microselectwidget.h"
0017 #include "projectmanager.h"
0018 #include "textdocument.h"
0019 
0020 #include <canvas.h>
0021 
0022 #include <KLineEdit>
0023 // #include <k3iconview.h>
0024 #include <KIconLoader>
0025 #include <KLocalizedString>
0026 
0027 #include <QCheckBox>
0028 #include <QDialogButtonBox>
0029 #include <QDir>
0030 #include <QFile>
0031 #include <QLabel>
0032 #include <QPushButton>
0033 #include <QVBoxLayout>
0034 // #include <q3paintdevicemetrics.h>
0035 
0036 #include <algorithm>
0037 
0038 #include <ui_newfilewidget.h>
0039 #include <ktechlab_debug.h>
0040 
0041 class NewFileWidget : public QWidget, public Ui::NewFileWidget
0042 {
0043 public:
0044     NewFileWidget(QWidget *parent)
0045         : QWidget(parent)
0046     {
0047         setupUi(this);
0048     }
0049 };
0050 
0051 NewFileDlg::NewFileDlg(QWidget *parent)
0052     : QDialog(parent)
0053 {
0054     setObjectName("newfiledlg");
0055     setModal(true);
0056     setWindowTitle(i18n("New File"));
0057 
0058     QVBoxLayout *mainLayout = new QVBoxLayout;
0059     setLayout(mainLayout);
0060 
0061     m_pMainParent = parent;
0062     m_pNewFileWidget = new NewFileWidget(this);
0063 
0064     m_pNewFileWidget->typeIconView->setSelectionMode(QAbstractItemView::SingleSelection /*Q3IconView::Single*/);
0065     // m_pNewFileWidget->typeIconView->setMode(K3IconView::Select); // 2017.12.01 - convert to qlistwidget
0066     m_pNewFileWidget->typeIconView->setIconSize(QSize(KIconLoader::SizeHuge, KIconLoader::SizeHuge));
0067 
0068     QList<QListWidgetItem *> items;
0069 
0070     // BEGIN insert icons
0071     QString text = QString("%1 (.asm)").arg(i18n("Assembly Code"));
0072     items << new QListWidgetItem(QIcon::fromTheme("source"), text, m_pNewFileWidget->typeIconView);
0073 
0074     text = "C (.c)";
0075     items << new QListWidgetItem(QIcon::fromTheme("text-x-csrc"), text, m_pNewFileWidget->typeIconView);
0076 
0077     text = QString("%1 (.circuit)").arg(i18n("Circuit"));
0078     items << new QListWidgetItem(QIcon::fromTheme("application-x-circuit"), text, m_pNewFileWidget->typeIconView);
0079 
0080     items << new QListWidgetItem(QIcon::fromTheme("application-x-flowcode"), "FlowCode (.flowcode)", m_pNewFileWidget->typeIconView);
0081 
0082 #ifdef MECHANICS
0083     items << new QListWidgetItem(QIcon::fromTheme(QIcon::fromTheme("exec")), "Mechanics (.mechanics)", m_pNewFileWidget->typeIconView);
0084 #endif
0085 
0086     items << new QListWidgetItem(QIcon::fromTheme("application-x-microbe"), "Microbe (.microbe)", m_pNewFileWidget->typeIconView);
0087     // END insert icons
0088 
0089     int minWidth = 20 + m_pNewFileWidget->typeIconView->spacing() * items.size();
0090     int minHeight = 20;
0091 
0092     const QList<QListWidgetItem *>::iterator end = items.end();
0093     for (QList<QListWidgetItem *>::iterator it = items.begin(); it != end; ++it) {
0094         //(*it)->setDragEnabled(false); // 2017.12.01 - use qlistwidget
0095         Qt::ItemFlags flags = (*it)->flags();
0096         flags &= (~Qt::ItemIsDragEnabled);
0097         (*it)->setFlags(flags);
0098 
0099         QList<QSize> listAvSizes = (*it)->icon().availableSizes();
0100         if (listAvSizes.isEmpty()) {
0101             qCWarning(KTL_LOG) << "no available sizes for " << (*it)->text();
0102         } else {
0103             qCDebug(KTL_LOG) << "W = " << (*it)->icon().availableSizes().first().width() << " H=" << (*it)->icon().availableSizes().first().height();
0104             minWidth += (*it)->icon().availableSizes().first().width() + 20;
0105             minHeight = std::max(minHeight, (*it)->icon().availableSizes().first().height() + 20);
0106         }
0107     }
0108     qCDebug(KTL_LOG) << "minW = " << minWidth << " minH=" << minHeight;
0109     m_pNewFileWidget->typeIconView->setMinimumSize(minWidth, minHeight);
0110     m_pNewFileWidget->typeIconView->setCurrentItem(items[3]);
0111     m_pNewFileWidget->addToProjectCheck->setChecked(ProjectManager::self()->currentProject());
0112     m_pNewFileWidget->addToProjectCheck->setEnabled(ProjectManager::self()->currentProject());
0113     microSelectWidget()->setAllowedFlowCodeSupport(MicroInfo::FullSupport | MicroInfo::PartialSupport);
0114 
0115     mainLayout->addWidget(m_pNewFileWidget);
0116 
0117     // Our behaviour is to have single click selects and double click accepts the dialog
0118     connect(m_pNewFileWidget->typeIconView, &QListWidget::itemSelectionChanged, this, &NewFileDlg::fileTypeChanged);
0119     connect(m_pNewFileWidget->typeIconView, &QListWidget::doubleClicked, this, &NewFileDlg::accept);
0120 
0121     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0122     mainLayout->addWidget(buttonBox);
0123 
0124     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0125     okButton->setDefault(true);
0126     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0127     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0128     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0129 
0130     setAcceptDrops(true);
0131 
0132     m_pNewFileWidget->typeIconView->adjustSize();
0133     m_pNewFileWidget->adjustSize();
0134     adjustSize();
0135 }
0136 
0137 void NewFileDlg::accept()
0138 {
0139     QDialog::accept();
0140 
0141     const QString fileText = m_pNewFileWidget->typeIconView->currentItem()->text();
0142 
0143     if (fileText.contains(".flowcode"))
0144         m_fileType = Document::dt_flowcode;
0145 
0146     else if (fileText.contains(".circuit"))
0147         m_fileType = Document::dt_circuit;
0148 
0149     else if (fileText.contains(".mechanics"))
0150         m_fileType = Document::dt_mechanics;
0151 
0152     else if (fileText.contains(".asm")) {
0153         m_fileType = Document::dt_text;
0154         m_codeType = TextDocument::ct_asm;
0155     }
0156 
0157     else if (fileText.contains(".basic") || fileText.contains(".microbe")) {
0158         m_fileType = Document::dt_text;
0159         m_codeType = TextDocument::ct_microbe;
0160     }
0161 
0162     else if (fileText.contains(".c")) {
0163         m_fileType = Document::dt_text;
0164         m_codeType = TextDocument::ct_c;
0165     }
0166 
0167     else
0168         m_fileType = Document::dt_text;
0169 
0170     m_bAddToProject = m_pNewFileWidget->addToProjectCheck->isChecked();
0171 
0172     m_microID = m_pNewFileWidget->m_pMicroSelect->micro();
0173 }
0174 
0175 void NewFileDlg::fileTypeChanged()
0176 {
0177     bool doEnableMicros = false;
0178     if (!m_pNewFileWidget->typeIconView->selectedItems().isEmpty()) {
0179         doEnableMicros = m_pNewFileWidget->typeIconView->selectedItems().first()->text().contains(".flowcode");
0180     }
0181     m_pNewFileWidget->m_pMicroSelect->setEnabled(doEnableMicros);
0182 }
0183 
0184 MicroSelectWidget *NewFileDlg::microSelectWidget() const
0185 {
0186     return m_pNewFileWidget->m_pMicroSelect;
0187 }
0188 
0189 #include "moc_newfiledlg.cpp"