diff --git a/CMakeLists.txt b/CMakeLists.txt index b8dfbba91..1b96b1378 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -280,6 +280,7 @@ endif(BUILD_RAYTRACING) #inferred from cmakelists.txt. appears to be working. if(BUILD_DRAWING) set(BUILD_PART ON) + set(BUILD_SPREADSHEET ON) endif(BUILD_DRAWING) #inferred from cmakelists.txt. appears to be working. diff --git a/src/Mod/Drawing/App/AppDrawing.cpp b/src/Mod/Drawing/App/AppDrawing.cpp index 2150b9ba2..9e0203a1c 100644 --- a/src/Mod/Drawing/App/AppDrawing.cpp +++ b/src/Mod/Drawing/App/AppDrawing.cpp @@ -23,6 +23,7 @@ #include "FeatureViewAnnotation.h" #include "FeatureViewSymbol.h" #include "FeatureProjection.h" +#include "FeatureViewSpreadsheet.h" #include "FeatureClip.h" #include "PageGroup.h" @@ -63,6 +64,7 @@ void DrawingExport initDrawing() Drawing::FeatureViewAnnotation ::init(); Drawing::FeatureViewSymbol ::init(); Drawing::FeatureClip ::init(); + Drawing::FeatureViewSpreadsheet ::init(); } } // extern "C" diff --git a/src/Mod/Drawing/App/CMakeLists.txt b/src/Mod/Drawing/App/CMakeLists.txt index 483f0886d..574deb2e4 100644 --- a/src/Mod/Drawing/App/CMakeLists.txt +++ b/src/Mod/Drawing/App/CMakeLists.txt @@ -17,6 +17,7 @@ link_directories(${OCC_LIBRARY_DIR}) set(Drawing_LIBS Part + Spreadsheet FreeCADApp ) @@ -37,6 +38,8 @@ SET(Features_SRCS FeatureClip.h PageGroup.cpp PageGroup.h + FeatureViewSpreadsheet.cpp + FeatureViewSpreadsheet.h ) SET(Drawing_SRCS diff --git a/src/Mod/Drawing/App/FeatureViewSpreadsheet.cpp b/src/Mod/Drawing/App/FeatureViewSpreadsheet.cpp new file mode 100644 index 000000000..b9382cc2b --- /dev/null +++ b/src/Mod/Drawing/App/FeatureViewSpreadsheet.cpp @@ -0,0 +1,252 @@ +/*************************************************************************** + * Copyright (c) Yorik van Havre (yorik@uncreated.net) 2015 * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library 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 Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" + +#ifndef _PreComp_ +# include +#endif + +#include +#include +#include +#include +#include +#include +#include "FeatureViewSpreadsheet.h" +#include +#include + +using namespace Drawing; +using namespace std; + +//=========================================================================== +// FeatureViewSpreadsheet +//=========================================================================== + +PROPERTY_SOURCE(Drawing::FeatureViewSpreadsheet, Drawing::FeatureView) + + +FeatureViewSpreadsheet::FeatureViewSpreadsheet(void) +{ + static const char *vgroup = "Drawing view"; + + ADD_PROPERTY_TYPE(CellStart ,("A1"),vgroup,App::Prop_None,"The top left cell of the range to display"); + ADD_PROPERTY_TYPE(CellEnd ,("B2"),vgroup,App::Prop_None,"The bottom right cell of the range to display"); + ADD_PROPERTY_TYPE(Font ,("Sans"),vgroup,App::Prop_None,"The name of the font to use"); + ADD_PROPERTY_TYPE(Color,(0.0f,0.0f,0.0f),vgroup,App::Prop_None,"The default color of the text and lines"); + ADD_PROPERTY_TYPE(Source ,(0),vgroup,App::Prop_None,"Spreadsheet to view"); + ADD_PROPERTY_TYPE(LineWidth,(0.35),vgroup,App::Prop_None,"The thickness of the cell lines"); + ADD_PROPERTY_TYPE(FontSize,(12.0),vgroup,App::Prop_None,"The size of the text"); +} + + +FeatureViewSpreadsheet::~FeatureViewSpreadsheet() +{ +} + +App::DocumentObjectExecReturn *FeatureViewSpreadsheet::execute(void) +{ + // quick tests + App::DocumentObject* link = Source.getValue(); + std::string scellstart = CellStart.getValue(); + std::string scellend = CellEnd.getValue(); + if (!link) + return new App::DocumentObjectExecReturn("No spreadsheet linked"); + if (!link->getTypeId().isDerivedFrom(Spreadsheet::Sheet::getClassTypeId())) + return new App::DocumentObjectExecReturn("The linked object is not a spreadsheet"); + if ( (scellstart.empty()) || (scellend.empty()) ) + return new App::DocumentObjectExecReturn("Empty cell value"); + + // build a list of available colums: A, B, C, ... AA, AB, ... ZY, ZZ. + std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + std::vector availcolumns; + for (int i=0; i<26; ++i) { + std::stringstream s; + s << alphabet[i]; + availcolumns.push_back(s.str()); + } + for (int i=0; i<26; ++i) { + for (int j=0; i<26; ++i) { + std::stringstream s; + s << alphabet[i] << alphabet[j]; + availcolumns.push_back(s.str()); + } + } + + // build rows range and columns range + std::vector columns; + std::vector rows; + try { + for (unsigned int i=0; i::const_iterator j = availcolumns.begin(); j != availcolumns.end(); ++j) { + if ( (*j) == startcol) { + if ( (*j) != endcol) { + valid = true; + } + } else { + if (valid) { + if ( (*j) == endcol) { + columns.push_back((*j)); + valid = false; + } else { + columns.push_back((*j)); + } + } + } + } + int endrow = std::atoi(scellend.substr(i,scellend.length()-1).c_str()); + for (int j=rows.back()+1; j<=endrow; ++j) { + rows.push_back(j); + } + } + } + } catch (std::exception) { + return new App::DocumentObjectExecReturn("Invalid cell range"); + } + + // create the containing group + std::string ViewName = Label.getValue(); + std::stringstream result,hr,hg,hb; + const App::Color& c = Color.getValue(); + hr << hex << setfill('0') << setw(2) << (int)(255.0*c.r); + hg << hex << setfill('0') << setw(2) << (int)(255.0*c.g); + hb << hex << setfill('0') << setw(2) << (int)(255.0*c.b); + result << "" << endl; + + // fill the cells + float rowoffset = 0.0; + float coloffset = 0.0; + float cellheight = 100; + float cellwidth = 100; + std::string celltext; + Spreadsheet::Sheet* sheet = static_cast(link); + std::vector skiplist; + for (std::vector::const_iterator col = columns.begin(); col != columns.end(); ++col) { + // create a group for each column + result << " " << endl; + for (std::vector::const_iterator row = rows.begin(); row != rows.end(); ++row) { + // get cell size + std::stringstream srow; + srow << (*row); + Spreadsheet::CellAddress address((*col) + srow.str()); + cellwidth = sheet->getColumnWidth(address.col()); + cellheight = sheet->getRowHeight(address.row()); + celltext = ""; + // get the text + App::Property* prop = sheet->getPropertyByName(address.toString().c_str()); + std::stringstream field; + if (prop != 0) { + if (prop->isDerivedFrom((App::PropertyQuantity::getClassTypeId()))) + field << static_cast(prop)->getValue(); + else if (prop->isDerivedFrom((App::PropertyFloat::getClassTypeId()))) + field << static_cast(prop)->getValue(); + else if (prop->isDerivedFrom((App::PropertyString::getClassTypeId()))) + field << static_cast(prop)->getValue(); + else + assert(0); + celltext = field.str(); + } + // get colors, style and span + std::string bcolor = "none"; + std::string fcolor = "#" + hr.str() + hg.str() + hb.str(); + std::string textstyle = ""; + Spreadsheet::Cell* cell = sheet->getCell(address); + if (cell) { + App::Color f,b; + std::set st; + int colspan, rowspan; + if (cell->getBackground(b)) { + std::stringstream br,bg,bb; + br << hex << setfill('0') << setw(2) << (int)(255.0*b.r); + bg << hex << setfill('0') << setw(2) << (int)(255.0*b.g); + bb << hex << setfill('0') << setw(2) << (int)(255.0*b.b); + bcolor = "#" + br.str() + bg.str() + bb.str(); + } + if (cell->getForeground(f)) { + std::stringstream fr,fg,fb; + fr << hex << setfill('0') << setw(2) << (int)(255.0*f.r); + fg << hex << setfill('0') << setw(2) << (int)(255.0*f.g); + fb << hex << setfill('0') << setw(2) << (int)(255.0*f.b); + fcolor = "#" + fr.str() + fg.str() + fb.str(); + } + if (cell->getStyle(st)) { + for (std::set::const_iterator i = st.begin(); i != st.end(); ++i) { + if ((*i) == "bold") + textstyle = textstyle + "font-weight: bold; "; + else if ((*i) == "italic") + textstyle = textstyle + "font-style: italic; "; + else if ((*i) == "underline") + textstyle = textstyle + "text-decoration: underline; "; + } + } + if (cell->getSpans(rowspan,colspan)) { + for (int i=0; i 0) + cellwidth = cellwidth + sheet->getColumnWidth(nextcell.col()); + if (j > 0) + cellheight = cellheight + sheet->getRowHeight(nextcell.row()); + if ( (i > 0) || (j > 0) ) + skiplist.push_back(nextcell.toString()); + } + } + } + } + // skip cell if found in skiplist + if (std::find(skiplist.begin(), skiplist.end(), address.toString()) == skiplist.end()) { + result << " " << endl + << " " << celltext << "" << endl; + } + rowoffset = rowoffset + cellheight; + } + result << " " << endl; + rowoffset = 0.0; + coloffset = coloffset + cellwidth; + } + + // close the containing group + result << "" << endl; + + // Apply the resulting fragment + ViewResult.setValue(result.str().c_str()); + + return App::DocumentObject::StdReturn; +} diff --git a/src/Mod/Drawing/App/FeatureViewSpreadsheet.h b/src/Mod/Drawing/App/FeatureViewSpreadsheet.h new file mode 100644 index 000000000..b6e478a16 --- /dev/null +++ b/src/Mod/Drawing/App/FeatureViewSpreadsheet.h @@ -0,0 +1,68 @@ +/*************************************************************************** + * Copyright (c) Yorik van Havre (yorik@uncreated.net 2015) * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library 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 Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#ifndef _FeatureViewSpreadsheet_h_ +#define _FeatureViewSpreadsheet_h_ + + +#include +#include +#include "FeatureView.h" + +namespace Drawing +{ + +/** Base class of all View Features in the drawing module + */ +class DrawingExport FeatureViewSpreadsheet : public FeatureView +{ + PROPERTY_HEADER(Drawing::FeatureView); + +public: + /// Constructor + FeatureViewSpreadsheet(void); + virtual ~FeatureViewSpreadsheet(); + App::PropertyLink Source; + App::PropertyString CellStart; + App::PropertyString CellEnd; + App::PropertyString Font; + App::PropertyColor Color; + App::PropertyFloat LineWidth; + App::PropertyFloat FontSize; + + /** @name methods overide Feature */ + //@{ + /// recalculate the Feature + virtual App::DocumentObjectExecReturn *execute(void); + //@} + + /// returns the type name of the ViewProvider + virtual const char* getViewProviderName(void) const { + return "DrawingGui::ViewProviderDrawingView"; + } +}; + +} //namespace Drawing + + +#endif diff --git a/src/Mod/Drawing/Gui/Command.cpp b/src/Mod/Drawing/Gui/Command.cpp index 46bb6b90a..741df37fb 100644 --- a/src/Mod/Drawing/Gui/Command.cpp +++ b/src/Mod/Drawing/Gui/Command.cpp @@ -35,6 +35,7 @@ #include #include +#include #include "DrawingView.h" @@ -727,6 +728,54 @@ bool CmdDrawingDraftView::isActive(void) } +//=========================================================================== +// Drawing_Spreadheet_View +//=========================================================================== + +DEF_STD_CMD_A(CmdDrawingSpreadsheetView); + +CmdDrawingSpreadsheetView::CmdDrawingSpreadsheetView() + : Command("Drawing_SpreadsheetView") +{ + // seting the + sGroup = QT_TR_NOOP("Drawing"); + sMenuText = QT_TR_NOOP("&Spreadsheet View"); + sToolTipText = QT_TR_NOOP("Inserts a view of a selected spreadsheet in the active drawing"); + sWhatsThis = "Drawing_SpreadsheetView"; + sStatusTip = QT_TR_NOOP("Inserts a view of a selected spreadsheet in the active drawing"); + sPixmap = "actions/drawing-spreadsheet"; +} + +void CmdDrawingSpreadsheetView::activated(int iMsg) +{ + const std::vector spreads = getSelection().getObjectsOfType(Spreadsheet::Sheet::getClassTypeId()); + if (spreads.size() != 1) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), + QObject::tr("Select exactly one Spreadsheet object.")); + return; + } + const std::vector pages = this->getDocument()->getObjectsOfType(Drawing::FeaturePage::getClassTypeId()); + if (pages.empty()) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No page found"), + QObject::tr("Create a page first.")); + return; + } + std::string SpreadName = spreads.front()->getNameInDocument(); + std::string PageName = pages.front()->getNameInDocument(); + openCommand("Create spreadsheet view"); + std::string FeatName = getUniqueObjectName("View"); + doCommand(Doc,"App.activeDocument().addObject('Drawing::FeatureViewSpreadsheet','%s')",FeatName.c_str()); + doCommand(Doc,"App.activeDocument().%s.Source = App.activeDocument().%s",FeatName.c_str(),SpreadName.c_str()); + doCommand(Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",PageName.c_str(),FeatName.c_str()); + updateActive(); + commitCommand(); +} + +bool CmdDrawingSpreadsheetView::isActive(void) +{ + return (getActiveGuiDocument() ? true : false); +} + void CreateDrawingCommands(void) { @@ -744,4 +793,5 @@ void CreateDrawingCommands(void) rcCmdMgr.addCommand(new CmdDrawingExportPage()); rcCmdMgr.addCommand(new CmdDrawingProjectShape()); rcCmdMgr.addCommand(new CmdDrawingDraftView()); + rcCmdMgr.addCommand(new CmdDrawingSpreadsheetView()); } diff --git a/src/Mod/Drawing/Gui/Resources/Drawing.qrc b/src/Mod/Drawing/Gui/Resources/Drawing.qrc index 5cc31ad3e..a4e367242 100644 --- a/src/Mod/Drawing/Gui/Resources/Drawing.qrc +++ b/src/Mod/Drawing/Gui/Resources/Drawing.qrc @@ -20,6 +20,7 @@ icons/actions/drawing-clip.svg icons/actions/drawing-symbol.svg icons/actions/drawing-draft-view.svg + icons/actions/drawing-spreadsheet.svg translations/Drawing_af.qm translations/Drawing_de.qm translations/Drawing_fi.qm diff --git a/src/Mod/Drawing/Gui/Resources/icons/actions/drawing-spreadsheet.svg b/src/Mod/Drawing/Gui/Resources/icons/actions/drawing-spreadsheet.svg new file mode 100644 index 000000000..67dcc956d --- /dev/null +++ b/src/Mod/Drawing/Gui/Resources/icons/actions/drawing-spreadsheet.svg @@ -0,0 +1,792 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Mod/Drawing/Gui/Workbench.cpp b/src/Mod/Drawing/Gui/Workbench.cpp index 1e4e7fdec..bd8e306ee 100644 --- a/src/Mod/Drawing/Gui/Workbench.cpp +++ b/src/Mod/Drawing/Gui/Workbench.cpp @@ -66,6 +66,7 @@ Gui::MenuItem* Workbench::setupMenuBar() const *part << "Drawing_Clip"; *part << "Drawing_Symbol"; *part << "Drawing_DraftView"; + *part << "Drawing_SpreadsheetView"; *part << "Drawing_ExportPage"; *part << "Separator"; *part << "Drawing_ProjectShape"; @@ -88,6 +89,7 @@ Gui::ToolBarItem* Workbench::setupToolBars() const *part << "Drawing_Clip"; *part << "Drawing_Symbol"; *part << "Drawing_DraftView"; + *part << "Drawing_SpreadsheetView"; *part << "Drawing_ExportPage"; return root; }