TEST (DescVisitor, SimpleGraphics) { SimpleGraphics s(new Rectangle(0,0,2,3)); DescriptionVisitor dv; s.accept(dv); CHECK(std::string("R(0,0,2,3)\n")==dv.getDescription()); }
void gui::SaveFileDialog() { QString savefilepath = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::currentPath(), tr("Text files (*.txt)")); QFile file(savefilepath); if(!file.open(QIODevice::WriteOnly|QIODevice::Text)) return; QTextStream out(&file); DescriptionVisitor dv; graphics->accept(dv); out << dv.getDescription().c_str(); file.close(); }
TEST (DescVisitor, CompositeGraphics) { CompositeGraphics g; g.add(new SimpleGraphics(new Circle(0,0,1))); g.add(new SimpleGraphics(new Square(-2,-2,2))); DescriptionVisitor dv; g.accept(dv); std::string ans("Comp R(-2,-2,3,3)\n"); ans += std::string(" C(0,0,1)\n"); ans += std::string(" S(-2,-2,2)\n"); CHECK(ans==dv.getDescription()); // std::cout << dv.getDescription() << std::endl; }
bool GraphicsModel::saveToFile(const char *filename) { if (root) { DescriptionVisitor visitor; this->root->accept(visitor); string contents = visitor.getDescription(); ofstream ofs; ofs.open(filename); if (ofs.is_open()) { ofs << contents; ofs.close(); return true; } } return false; }
TEST (DescVisitor, CompositeGraphicsWithComposite) { CompositeGraphics g; g.add(new SimpleGraphics(new Circle(0,0,1))); g.add(new SimpleGraphics(new Square(-2,-2,2))); SimpleGraphics r(new Rectangle (-1,-1,1,3)); CompositeGraphics h; h.add(&g); h.add(&r); DescriptionVisitor dv; h.accept(dv); std::string ans("Comp R(-2,-2,3,4)\n"); ans += std::string(" Comp R(-2,-2,3,3)\n"); ans += std::string(" C(0,0,1)\n"); ans += std::string(" S(-2,-2,2)\n"); ans += std::string(" R(-1,-1,1,3)\n"); CHECK(ans==dv.getDescription()); // std::cout << dv.getDescription() << std::endl; }