File size: 24,795 Bytes
985c397 | 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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) Eivind Kvedalen (eivind@kvedalen.name) 2015-2016 *
* *
* 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 <QPalette>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QPrinter>
#include <QTextDocument>
#include <App/Application.h>
#include <App/DocumentObject.h>
#include <App/Range.h>
#include <Base/Tools.h>
#include <Gui/Application.h>
#include <Gui/Command.h>
#include <Gui/CommandT.h>
#include <Gui/Document.h>
#include <Gui/FileDialog.h>
#include <Gui/MainWindow.h>
#include <Gui/PreferencePages/DlgSettingsPDF.h>
#include <Mod/Spreadsheet/App/Sheet.h>
#include <Mod/Spreadsheet/App/SheetPy.h>
#include "LineEdit.h"
#include "SpreadsheetDelegate.h"
#include "SpreadsheetView.h"
#include "ZoomableView.h"
#include "qtcolorpicker.h"
#include "ui_Sheet.h"
using namespace SpreadsheetGui;
using namespace Spreadsheet;
using namespace Gui;
using namespace App;
namespace sp = std::placeholders;
/* TRANSLATOR SpreadsheetGui::SheetView */
TYPESYSTEM_SOURCE_ABSTRACT(SpreadsheetGui::SheetView, Gui::MDIView)
SheetView::SheetView(Gui::Document* pcDocument, App::DocumentObject* docObj, QWidget* parent)
: MDIView(pcDocument, parent)
, sheet(static_cast<Sheet*>(docObj))
{
// Set up ui
model = new SheetModel(static_cast<Sheet*>(docObj));
ui = new Ui::Sheet();
QWidget* w = new QWidget(this);
ui->setupUi(w);
setCentralWidget(w);
new ZoomableView(ui);
delegate = new SpreadsheetDelegate(sheet);
ui->cells->setModel(model);
ui->cells->setItemDelegate(delegate);
ui->cells->setSheet(sheet);
// Connect signals
connect(
ui->cells->selectionModel(),
&QItemSelectionModel::currentChanged,
this,
&SheetView::currentChanged
);
connect(
qobject_cast<SheetViewHeader*>(ui->cells->horizontalHeader()),
&SheetViewHeader::resizeFinished,
this,
&SheetView::columnResizeFinished
);
connect(ui->cells->horizontalHeader(), &QHeaderView::sectionResized, this, &SheetView::columnResized);
connect(
qobject_cast<SheetViewHeader*>(ui->cells->verticalHeader()),
&SheetViewHeader::resizeFinished,
this,
&SheetView::rowResizeFinished
);
connect(ui->cells->verticalHeader(), &QHeaderView::sectionResized, this, &SheetView::rowResized);
connect(delegate, &SpreadsheetDelegate::finishedWithKey, this, &SheetView::editingFinishedWithKey);
connect(ui->cellContent, &ExpressionLineEdit::returnPressed, this, [this]() {
confirmContentChanged(ui->cellContent->text());
});
connect(ui->cellAlias, &ExpressionLineEdit::editingFinished, this, [this]() {
confirmAliasChanged(ui->cellAlias->text());
});
connect(ui->cellAlias, &LineEdit::textEdited, this, &SheetView::aliasChanged);
// NOLINTBEGIN
columnWidthChangedConnection = sheet->columnWidthChanged.connect(
std::bind(&SheetView::resizeColumn, this, sp::_1, sp::_2)
);
rowHeightChangedConnection = sheet->rowHeightChanged.connect(
std::bind(&SheetView::resizeRow, this, sp::_1, sp::_2)
);
// NOLINTEND
connect(model, &QAbstractItemModel::dataChanged, this, &SheetView::modelUpdated);
QPalette palette = ui->cells->palette();
palette.setColor(QPalette::Base, QColor(255, 255, 255));
palette.setColor(QPalette::Text, QColor(0, 0, 0));
ui->cells->setPalette(palette);
QList<QtColorPicker*> bgList = Gui::getMainWindow()->findChildren<QtColorPicker*>(
QStringLiteral("Spreadsheet_BackgroundColor")
);
if (!bgList.empty()) {
bgList[0]->setCurrentColor(palette.color(QPalette::Base));
}
QList<QtColorPicker*> fgList = Gui::getMainWindow()->findChildren<QtColorPicker*>(
QStringLiteral("Spreadsheet_ForegroundColor")
);
if (!fgList.empty()) {
fgList[0]->setCurrentColor(palette.color(QPalette::Text));
}
// Set document object to create auto completer
ui->cellContent->setDocumentObject(sheet);
ui->cellAlias->setDocumentObject(sheet);
ui->cellContent->setPrefix('=');
}
SheetView::~SheetView()
{
Gui::Application::Instance->detachView(this);
delete ui;
delete model;
delete delegate;
}
bool SheetView::onMsg(const char* pMsg, const char**)
{
if (strcmp("Undo", pMsg) == 0) {
getGuiDocument()->undo(1);
App::Document* doc = getAppDocument();
if (doc) {
doc->recompute();
}
return true;
}
else if (strcmp("Redo", pMsg) == 0) {
getGuiDocument()->redo(1);
App::Document* doc = getAppDocument();
if (doc) {
doc->recompute();
}
return true;
}
else if (strcmp("Save", pMsg) == 0) {
getGuiDocument()->save();
return true;
}
else if (strcmp("SaveAs", pMsg) == 0) {
getGuiDocument()->saveAs();
return true;
}
else if (strcmp("Std_Delete", pMsg) == 0) {
std::vector<Range> ranges = selectedRanges();
if (sheet->hasCell(ranges)) {
Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Clear Cells"));
std::vector<Range>::const_iterator i = ranges.begin();
for (; i != ranges.end(); ++i) {
FCMD_OBJ_CMD(sheet, "clear('" << i->rangeString() << "')");
}
Gui::Command::commitCommand();
Gui::Command::doCommand(Gui::Command::Doc, "App.ActiveDocument.recompute()");
}
return true;
}
else if (strcmp("Cut", pMsg) == 0) {
ui->cells->cutSelection();
return true;
}
else if (strcmp("Copy", pMsg) == 0) {
ui->cells->copySelection();
return true;
}
else if (strcmp("Paste", pMsg) == 0) {
ui->cells->pasteClipboard();
return true;
}
else {
return false;
}
}
bool SheetView::onHasMsg(const char* pMsg) const
{
if (strcmp("Undo", pMsg) == 0) {
App::Document* doc = getAppDocument();
return doc && doc->getAvailableUndos() > 0;
}
if (strcmp("Redo", pMsg) == 0) {
App::Document* doc = getAppDocument();
return doc && doc->getAvailableRedos() > 0;
}
if (strcmp("Save", pMsg) == 0) {
return true;
}
if (strcmp("SaveAs", pMsg) == 0) {
return true;
}
if (strcmp("Cut", pMsg) == 0) {
return true;
}
if (strcmp("Copy", pMsg) == 0) {
return true;
}
if (strcmp("Paste", pMsg) == 0) {
return true;
}
if (strcmp(pMsg, "Print") == 0) {
return true;
}
if (strcmp(pMsg, "PrintPreview") == 0) {
return true;
}
if (strcmp(pMsg, "PrintPdf") == 0) {
return true;
}
else if (strcmp("AllowsOverlayOnHover", pMsg) == 0) {
return true;
}
return false;
}
/**
* Shows the printer dialog.
*/
void SheetView::print()
{
QPrinter printer(QPrinter::ScreenResolution);
printer.setPageOrientation(QPageLayout::Landscape);
printer.setFullPage(true);
QPrintDialog dlg(&printer, this);
if (dlg.exec() == QDialog::Accepted) {
print(&printer);
}
}
void SheetView::printPreview()
{
QPrinter printer(QPrinter::ScreenResolution);
printer.setPageOrientation(QPageLayout::Landscape);
QPrintPreviewDialog dlg(&printer, this);
connect(&dlg, &QPrintPreviewDialog::paintRequested, this, qOverload<QPrinter*>(&SheetView::print));
dlg.exec();
}
void SheetView::print(QPrinter* printer)
{
#if 0
ui->cells->render(printer);
#endif
std::unique_ptr<QTextDocument> document = std::make_unique<QTextDocument>();
document->setHtml(ui->cells->toHtml());
document->print(printer);
}
/**
* Prints the document into a Pdf file.
*/
void SheetView::printPdf()
{
QString filename = FileDialog::getSaveFileName(
this,
tr("Export PDF"),
QString(),
QStringLiteral("%1 (*.pdf)").arg(tr("PDF file"))
);
if (!filename.isEmpty()) {
QPrinter printer(QPrinter::ScreenResolution);
// setPdfVersion sets the printed PDF Version to what is chosen in
// Preferences/Import-Export/PDF more details under:
// https://www.kdab.com/creating-pdfa-documents-qt/
printer.setPdfVersion(Gui::Dialog::DlgSettingsPDF::evaluatePDFVersion());
printer.setPageOrientation(QPageLayout::Landscape);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(filename);
printer.setCreator(QString::fromStdString(App::Application::getNameWithVersion()));
print(&printer);
}
}
void SheetView::setCurrentCell(QString str)
{
Q_UNUSED(str);
updateContentLine();
updateAliasLine();
}
void SheetView::updateContentLine()
{
QModelIndex i = ui->cells->currentIndex();
if (i.isValid()) {
std::string str;
if (const auto* cell = sheet->getCell(CellAddress(i.row(), i.column()))) {
(void)cell->getStringContent(str);
}
ui->cellContent->setText(QString::fromUtf8(str.c_str()));
ui->cellContent->setEnabled(true);
// Update completer model; for the time being, we do this by setting the document object of
// the input line.
ui->cellContent->setDocumentObject(sheet);
}
}
void SheetView::updateAliasLine()
{
QModelIndex i = ui->cells->currentIndex();
if (i.isValid()) {
std::string str;
if (const auto* cell = sheet->getCell(CellAddress(i.row(), i.column()))) {
(void)cell->getAlias(str);
}
ui->cellAlias->setText(QString::fromUtf8(str.c_str()));
ui->cellAlias->setEnabled(true);
// Update completer model; for the time being, we do this by setting the document object of
// the input line.
ui->cellAlias->setDocumentObject(sheet);
}
}
void SheetView::columnResizeFinished()
{
if (newColumnSizes.empty()) {
return;
}
blockSignals(true);
for (auto& v : newColumnSizes) {
sheet->setColumnWidth(v.first, v.second);
}
blockSignals(false);
newColumnSizes.clear();
}
void SheetView::rowResizeFinished()
{
if (newRowSizes.empty()) {
return;
}
blockSignals(true);
for (auto& v : newRowSizes) {
sheet->setRowHeight(v.first, v.second);
}
blockSignals(false);
newRowSizes.clear();
}
void SheetView::modelUpdated(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
const QModelIndex& current = ui->cells->currentIndex();
if (current < topLeft || bottomRight < current) {
return;
}
updateContentLine();
updateAliasLine();
}
void SheetView::columnResized(int col, int oldSize, int newSize)
{
Q_UNUSED(oldSize);
newColumnSizes[col] = newSize;
}
void SheetView::rowResized(int row, int oldSize, int newSize)
{
Q_UNUSED(oldSize);
newRowSizes[row] = newSize;
}
void SheetView::resizeColumn(int col, int newSize)
{
if (ui->cells->horizontalHeader()->sectionSize(col) != newSize) {
ui->cells->setColumnWidth(col, newSize);
}
}
void SheetView::resizeRow(int col, int newSize)
{
if (ui->cells->verticalHeader()->sectionSize(col) != newSize) {
ui->cells->setRowHeight(col, newSize);
}
}
void SheetView::editingFinishedWithKey(int key, Qt::KeyboardModifiers modifiers)
{
QModelIndex i = ui->cells->currentIndex();
if (i.isValid()) {
ui->cells->finishEditWithMove(key, modifiers);
}
}
void SheetView::confirmAliasChanged(const QString& text)
{
bool aliasOkay = true;
ui->cellAlias->setDocumentObject(sheet);
if (text.length() != 0 && !sheet->isValidAlias(text.toStdString())) {
aliasOkay = false;
}
QModelIndex i = ui->cells->currentIndex();
if (const auto* cell = sheet->getNewCell(CellAddress(i.row(), i.column()))) {
if (!aliasOkay) {
// do not show error message if failure to set new alias is because it is already the
// same string
std::string current_alias;
(void)cell->getAlias(current_alias);
if (text != QString::fromUtf8(current_alias.c_str())) {
Base::Console().error("Unable to set alias: %s\n", text.toStdString().c_str());
}
}
else {
std::string address = CellAddress(i.row(), i.column()).toString();
Gui::cmdAppObjectArgs(sheet, "setAlias('%s', '%s')", address, text.toStdString());
Gui::cmdAppDocument(sheet->getDocument(), "recompute()");
ui->cells->setFocus();
}
}
}
void SheetView::confirmContentChanged(const QString& text)
{
QModelIndex i = ui->cells->currentIndex();
ui->cells->model()->setData(i, QVariant(text), Qt::EditRole);
ui->cells->setFocus();
}
void SheetView::aliasChanged(const QString& text)
{
// check live the input and highlight if the user input invalid characters
bool aliasOk = true;
static auto originalStylesheet = ui->cellAlias->styleSheet();
QString warningColor;
if (qApp->styleSheet().contains(QLatin1String("dark"), Qt::CaseInsensitive)) {
warningColor = QLatin1String("rgb(255,90,90)"); // Light red for dark mode
}
else {
warningColor = QLatin1String("rgb(200,0,0)"); // Dark red for light mode
}
if (!text.isEmpty() && !sheet->isValidAlias(text.toStdString())) {
aliasOk = false;
}
if (!aliasOk) {
// change tooltip and make text color red
ui->cellAlias->setToolTip(QObject::tr("Alias contains invalid characters!"));
ui->cellAlias->setStyleSheet(QLatin1String("color:") + warningColor);
}
else {
// go back to normal
ui->cellAlias->setToolTip(
QObject::tr(
"Refer to cell by alias, for example\nSpreadsheet.my_alias_name instead of "
"Spreadsheet.B1"
)
);
ui->cellAlias->setStyleSheet(originalStylesheet);
}
}
void SheetView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
{
Q_UNUSED(current);
Q_UNUSED(previous);
updateContentLine();
updateAliasLine();
}
void SheetView::updateCell(const App::Property* prop)
{
try {
if (prop == &sheet->Label) {
QString cap = QString::fromUtf8(sheet->Label.getValue());
setWindowTitle(cap);
}
CellAddress address;
if (!sheet->getCellAddress(prop, address)) {
return;
}
if (currentIndex().row() == address.row() && currentIndex().column() == address.col()) {
updateContentLine();
updateAliasLine();
}
}
catch (...) {
// Property is not a cell
return;
}
}
std::vector<Range> SheetView::selectedRanges() const
{
return ui->cells->selectedRanges();
}
QModelIndexList SheetView::selectedIndexes() const
{
return ui->cells->selectionModel()->selectedIndexes();
}
QModelIndexList SheetView::selectedIndexesRaw() const
{
return ui->cells->selectedIndexesRaw();
}
void SpreadsheetGui::SheetView::select(App::CellAddress cell, QItemSelectionModel::SelectionFlags flags)
{
ui->cells->selectionModel()->select(model->index(cell.row(), cell.col()), flags);
}
void SpreadsheetGui::SheetView::select(
App::CellAddress topLeft,
App::CellAddress bottomRight,
QItemSelectionModel::SelectionFlags flags
)
{
ui->cells->selectionModel()->select(
QItemSelection(
model->index(topLeft.row(), topLeft.col()),
model->index(bottomRight.row(), bottomRight.col())
),
flags
);
}
void SheetView::deleteSelection()
{
ui->cells->deleteSelection();
}
QModelIndex SheetView::currentIndex() const
{
return ui->cells->currentIndex();
}
void SpreadsheetGui::SheetView::setCurrentIndex(App::CellAddress cell) const
{
ui->cells->setCurrentIndex(model->index(cell.row(), cell.col()));
}
PyObject* SheetView::getPyObject()
{
if (!pythonObject) {
pythonObject = new SheetViewPy(this);
}
Py_INCREF(pythonObject);
return pythonObject;
}
void SheetView::deleteSelf()
{
Gui::MDIView::deleteSelf();
}
// ----------------------------------------------------------
void SheetViewPy::init_type()
{
behaviors().name("SheetViewPy");
behaviors().doc("Python binding class for the Sheet view class");
// you must have overwritten the virtual functions
behaviors().supportRepr();
behaviors().supportGetattr();
behaviors().supportSetattr();
add_varargs_method(
"selectedRanges",
&SheetViewPy::selectedRanges,
"selectedRanges(): Get a list of all selected ranges"
);
add_varargs_method(
"selectedCells",
&SheetViewPy::selectedCells,
"selectedCells(): Get a list of all selected cells"
);
add_varargs_method(
"select",
&SheetViewPy::select,
"select(cell,flags): Select (or deselect) the given cell, applying "
"QItemSelectionModel.SelectionFlags\nselect(topLeft,bottomRight,flags): Select (or "
"deselect) the given range, applying QItemSelectionModel.SelectionFlags"
);
add_varargs_method(
"currentIndex",
&SheetViewPy::currentIndex,
"currentIndex(): Get the current index"
);
add_varargs_method(
"setCurrentIndex",
&SheetViewPy::setCurrentIndex,
"setCurrentIndex(cell): Set the current index to the named cell (e.g. 'A1')"
);
add_varargs_method("getSheet", &SheetViewPy::getSheet, "getSheet()");
add_varargs_method("cast_to_base", &SheetViewPy::cast_to_base, "cast_to_base() cast to MDIView class");
behaviors().readyType();
}
SheetViewPy::SheetViewPy(SheetView* mdi)
: base(mdi)
{}
SheetViewPy::~SheetViewPy() = default;
Py::Object SheetViewPy::repr()
{
if (!getSheetViewPtr()) {
throw Py::RuntimeError("Cannot print representation of deleted object");
}
return Py::String("SheetView");
}
// Since with PyCXX it's not possible to make a sub-class of MDIViewPy
// a trick is to use MDIViewPy as class member and override getattr() to
// join the attributes of both classes. This way all methods of MDIViewPy
// appear for SheetViewPy, too.
Py::Object SheetViewPy::getattr(const char* attr)
{
if (!getSheetViewPtr()) {
std::ostringstream s_out;
s_out << "Cannot access attribute '" << attr << "' of deleted object";
throw Py::RuntimeError(s_out.str());
}
std::string name(attr);
if (name == "__dict__" || name == "__class__") {
Py::Dict dict_self(BaseType::getattr("__dict__"));
Py::Dict dict_base(base.getattr("__dict__"));
for (const auto& it : dict_base) {
dict_self.setItem(it.first, it.second);
}
return dict_self;
}
try {
return BaseType::getattr(attr);
}
catch (Py::AttributeError& e) {
e.clear();
return base.getattr(attr);
}
}
SheetView* SheetViewPy::getSheetViewPtr()
{
return qobject_cast<SheetView*>(base.getMDIViewPtr());
}
Py::Object SheetViewPy::getSheet(const Py::Tuple& args)
{
if (!PyArg_ParseTuple(args.ptr(), "")) {
throw Py::Exception();
}
return Py::asObject(new Spreadsheet::SheetPy(getSheetViewPtr()->getSheet()));
}
Py::Object SheetViewPy::cast_to_base(const Py::Tuple&)
{
return Gui::MDIViewPy::create(base.getMDIViewPtr());
}
Py::Object SheetViewPy::selectedRanges(const Py::Tuple& args)
{
if (!PyArg_ParseTuple(args.ptr(), "")) {
throw Py::Exception();
}
SheetView* sheetView = getSheetViewPtr();
std::vector<App::Range> ranges = sheetView->selectedRanges();
Py::List list;
for (const auto& range : ranges) {
list.append(Py::String(range.rangeString()));
}
return list;
}
Py::Object SheetViewPy::selectedCells(const Py::Tuple& args)
{
if (!PyArg_ParseTuple(args.ptr(), "")) {
throw Py::Exception();
}
SheetView* sheetView = getSheetViewPtr();
QModelIndexList cells = sheetView->selectedIndexes();
Py::List list;
for (const auto& cell : cells) {
list.append(Py::String(App::CellAddress(cell.row(), cell.column()).toString()));
}
return list;
}
Py::Object SheetViewPy::select(const Py::Tuple& _args)
{
SheetView* sheetView = getSheetViewPtr();
Py::Sequence args(_args.ptr());
const char* cell;
const char* topLeft;
const char* bottomRight;
int flags = 0;
if (args.size() == 2 && PyArg_ParseTuple(_args.ptr(), "si", &cell, &flags)) {
sheetView->select(
App::CellAddress(cell),
static_cast<QItemSelectionModel::SelectionFlags>(flags)
);
}
else if (args.size() == 3
&& PyArg_ParseTuple(_args.ptr(), "ssi", &topLeft, &bottomRight, &flags)) {
sheetView->select(
App::CellAddress(topLeft),
App::CellAddress(bottomRight),
static_cast<QItemSelectionModel::SelectionFlags>(flags)
);
}
else {
if (args.size() == 2) {
throw Base::TypeError(
"Expects the arguments to be a cell name (e.g. 'A1') and "
"QItemSelectionModel.SelectionFlags"
);
}
else if (args.size() == 3) {
throw Base::TypeError(
"Expects the arguments to be a cell name (e.g. 'A1'), a second "
"cell name (e.g. 'B5'), and QItemSelectionModel.SelectionFlags"
);
}
else {
throw Base::TypeError(
"Wrong arguments to select: specify either a cell, or two cells "
"(for a range), and QItemSelectionModel.SelectionFlags"
);
}
}
return Py::None();
}
Py::Object SheetViewPy::currentIndex(const Py::Tuple& args)
{
if (!PyArg_ParseTuple(args.ptr(), "")) {
throw Py::Exception();
}
SheetView* sheetView = getSheetViewPtr();
auto index = sheetView->currentIndex();
Py::String str(App::CellAddress(index.row(), index.column()).toString());
return str;
}
Py::Object SheetViewPy::setCurrentIndex(const Py::Tuple& args)
{
SheetView* sheetView = getSheetViewPtr();
const char* cell;
if (PyArg_ParseTuple(args.ptr(), "s", &cell)) {
sheetView->setCurrentIndex(App::CellAddress(cell));
}
return Py::None();
}
#include "moc_SpreadsheetView.cpp"
|