1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/*
VeroRoute - Qt based Veroboard/Perfboard/PCB layout & routing application.
Copyright (C) 2017 Alex Lawrow ( dralx@users.sourceforge.net )
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 "bomdialog.h"
#include "ui_bomdialog.h"
#include "mainwindow.h"
#include <QtGlobal>
BomDialog::BomDialog(MainWindow* parent)
: QDialog(parent)
, ui(new Ui_BomDialog)
, m_pMainWindow(parent)
{
ui->setupUi(this);
#ifdef VEROROUTE_ANDROID
ui->tableWidget->verticalScrollBar()->setStyleSheet( ANDROID_VSCROLL_WIDTH );
ui->tableWidget->horizontalScrollBar()->setStyleSheet( ANDROID_HSCROLL_HEIGHT );
#endif
QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(WriteToFile()));
QObject::connect(this, SIGNAL(rejected()), m_pMainWindow, SLOT(UpdateControls())); // Close using X button
}
BomDialog::~BomDialog()
{
delete ui;
}
struct IsEarlierInBOM
{
bool operator()(const Component* pA, const Component* pB) const
{
// First order by type ...
if ( pA->GetType() != pB->GetType() ) return static_cast<int>(pA->GetType()) < static_cast<int>(pB->GetType());
// ... then by value string comparison
const int i = pA->GetValueStr().compare( pB->GetValueStr() );
if ( i != 0 ) return i < 0;
// ... then by name length
if ( pA->GetNameStr().length() != pB->GetNameStr().length() ) return pA->GetNameStr().length() < pB->GetNameStr().length();
// ... then by name string comparison
return pA->GetNameStr().compare( pB->GetNameStr() ) < 0;
}
};
void BomDialog::Update()
{
std::vector<const Component*> pComps; // Make list of pointers to all components for the B.O.M.
for (const auto& mapObj : m_pMainWindow->m_board.GetCompMgr().GetMapIdToComp())
{
const Component& comp = mapObj.second;
switch( comp.GetType() )
{
case COMP::VERO_NUMBER:
case COMP::VERO_LETTER:
case COMP::MARK:
case COMP::PAD:
case COMP::PAD_FLYINGWIRE:
case COMP::WIRE: break; // Not true components for BOM
default: pComps.push_back(&comp);
}
}
std::stable_sort(pComps.begin(), pComps.end(), IsEarlierInBOM()); // Sort the list appropriately
// Set up the table
ui->tableWidget->clear();
ui->tableWidget->setColumnCount(4);
ui->tableWidget->setColumnWidth(0,190);
ui->tableWidget->setColumnWidth(1,310);
ui->tableWidget->setColumnWidth(2,100);
ui->tableWidget->setColumnWidth(3,80); // Allow for vertical scroll bar
m_tableHeader << "Name" << "Type" << "Value" << "Quantity";
ui->tableWidget->setHorizontalHeaderLabels(m_tableHeader);
ui->tableWidget->verticalHeader()->setVisible(false);
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection);
ui->tableWidget->setShowGrid(true);
// Populate the table with data
int numRows(0);
for (int iLoop = 0; iLoop < 2; iLoop++) // First pass only counts number of rows
{
if ( iLoop == 1 ) // If second pass ...
ui->tableWidget->setRowCount(numRows); // ... set row count
std::string rowNames(""), rowType(""), rowValue("");
int row(-1), rowQuantity(0);
for (const auto& p : pComps)
{
const bool bLast = ( p == pComps.back() );
auto& strNewValue = p->GetValueStr();
auto strNewType = CompTypes::GetFamilyStr( p->GetType() );
if ( !StringHelper::IsEmptyStr(strNewType) ) strNewType += ": ";
strNewType += p->GetFullTypeStr();
const bool bNewRow = ( row == -1) || ( strNewValue != rowValue ) || ( strNewType != rowType );
if ( bNewRow )
{
if ( row != -1 && iLoop == 1 ) // If have a previous row ...
{
// Write previous row to table. Note: No memory leak since setItem() takes ownership.
ui->tableWidget->setItem(row, 0, new QTableWidgetItem(QString::fromStdString(rowNames)));
ui->tableWidget->setItem(row, 1, new QTableWidgetItem(QString::fromStdString(rowType)));
ui->tableWidget->setItem(row, 2, new QTableWidgetItem(QString::fromStdString(rowValue)));
ui->tableWidget->setItem(row, 3, new QTableWidgetItem(QString::number(rowQuantity)));
}
// Initialise data for new row
row++;
rowQuantity = 1;
rowNames = p->GetNameStr();
rowValue = strNewValue;
rowType = strNewType;
}
else
{
// Update data for row
rowQuantity++;
rowNames = rowNames + ", " + p->GetNameStr();
}
if ( bLast && iLoop == 1 ) // Very last component in the B.O.M.
{
// Write current row to table. Note: No memory leak since setItem() takes ownership.
ui->tableWidget->setItem(row, 0, new QTableWidgetItem(QString::fromStdString(rowNames)));
ui->tableWidget->setItem(row, 1, new QTableWidgetItem(QString::fromStdString(rowType)));
ui->tableWidget->setItem(row, 2, new QTableWidgetItem(QString::fromStdString(rowValue)));
ui->tableWidget->setItem(row, 3, new QTableWidgetItem(QString::number(rowQuantity)));
}
}
numRows = row + 1;
}
ui->pushButton->setDisabled( pComps.empty() );
}
void BomDialog::WriteToFile()
{
#ifdef VEROROUTE_ANDROID
const QString name = m_pMainWindow->GetFileName().isEmpty() ? QString("Circuit.vrt") : m_pMainWindow->GetFileName();
QString defaultName = StringHelper::ReplaceSuffix(name, QString("txt")); // Replace vrt with txt
QFile file(defaultName);
if ( file.exists() )
defaultName.clear();
else
defaultName = StringHelper::GetTidyFileName(defaultName);
QMessageBox::information(this, tr("Information"), tr("You must now select or enter a filename ending in .txt"));
const QString fileName = m_pMainWindow->GetSaveFileName(defaultName, tr("Text (*.txt);;All Files (*)"), QString("txt"));
#else
const QString fileName = m_pMainWindow->GetSaveFileName(tr("Choose a TXT file"), tr("Text (*.txt);;All Files (*)"), QString("txt"));
#endif
if ( !fileName.isEmpty() )
{
QFile file;
file.setFileName( fileName ) ;
QTextStream os;
const bool bOK = file.open(QIODevice::WriteOnly);
if ( bOK )
{
os.setDevice(&file);
#if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
os << "Name" << "\t" << "Type" << "\t" << "Value" << "\t" << "Quantity" << Qt::endl;
#else
os << "Name" << "\t" << "Type" << "\t" << "Value" << "\t" << "Quantity" << endl;
#endif
const int numRows = ui->tableWidget->rowCount();
const int numCols = ui->tableWidget->columnCount();
for (int i = 0; i < numRows; i++)
{
for(int j = 0; j < numCols; j++)
{
os << ui->tableWidget->item(i,j)->text();
if ( j != numCols - 1 ) os << "\t";
}
#if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
os << Qt::endl;
#else
os << endl;
#endif
}
#if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
os << Qt::endl;
#else
os << endl;
#endif
file.close();
}
else
QMessageBox::information(this, tr("Unable to save file"), fileName);
}
}
void BomDialog::keyPressEvent(QKeyEvent* event)
{
#ifndef VEROROUTE_ANDROID
m_pMainWindow->specialKeyPressEvent(event);
#endif
QDialog::keyPressEvent(event);
event->accept();
}
void BomDialog::keyReleaseEvent(QKeyEvent* event)
{
#ifdef VEROROUTE_ANDROID
if ( event->key() == Qt::Key_Back )
{
QTimer::singleShot(0, m_pMainWindow, SLOT(HideBomDialog()));
return event->accept();
}
#else
m_pMainWindow->commonKeyReleaseEvent(event);
#endif
QDialog::keyReleaseEvent(event);
event->accept();
}
|