Beispiel #1
0
void MainWindow::open() {
//    std::cout << "opening file " << std::endl;
     QFileDialog::Options options;
     QString selectedFilter;
     QString fileName = QFileDialog::getOpenFileName(this,
                                 tr("open file"),
                                 QString(""),
                                 tr("All Files (*);;Text Files (*.txt)"),
                                 &selectedFilter,
                                 options);
     if (!fileName.isEmpty()) {
         std::ifstream data(fileName.toStdString().c_str(), std::ios_base::in);
         while(!data.eof()){
             bool ok;
             std::string line;
             std::getline(data,line);
//             std::cout << "line = " << line << std::endl;
             if (line == "") continue;
             std::cout << "ShapeContainer" << std::endl;
             ShapeContainer *cont = new ShapeContainer();
             QStringList cont_list = QString(line.c_str()).split(" ");
             QStringList cont_par = cont_list[1].split(",");
             cont->setLocation(cont_par.at(0).toFloat(),cont_par.at(1).toFloat());
             cont->setColor(cont_par.at(4).toInt(&ok,10),cont_par.at(5).toInt(&ok,10),cont_par.at(6).toInt(&ok,10));
             cont->setSelected(true);
             while( line != "***") {
//                 std::cout << line << std::endl;
                 QStringList list = QString(line.c_str()).split(" ");
                 QStringList par = list[1].split(",");
                 if (list[0] == "Shape"){
                     float x = par.at(0).toFloat(), y = par.at(1).toFloat(), w = par.at(2).toFloat(), h = par.at(3).toFloat();
                     int r = par.at(4).toInt(&ok,10), g = par.at(5).toInt(&ok,10), b = par.at(6).toInt(&ok,10);
                     std::cout << "Shape("<<x<<","<<y<<","<<","<<w<<","<<h<<","<<r<<","<<g<<","<<b<<")"<<std::endl;
                     Shape* shape = new Shape(x,y,w,h,r,g,b);
                     cont->add(shape);

                 } else if (list[0] == "Rectangle") {
                     float x = par.at(0).toFloat(), y = par.at(1).toFloat(), w = par.at(2).toFloat(), h = par.at(3).toFloat();
                     int r = par.at(4).toInt(&ok,10), g = par.at(5).toInt(&ok,10), b = par.at(6).toInt(&ok,10);
                     std::cout << "Rectangle(" << x << "," << y << "," << w << "," <<h<<","<<r<<","<<g<<","<<b<<")"<<std::endl;
                     Shape *rect = new Rectangle(x,y,w,h,r,g,b);
                     cont->add(rect);
                 } else if (list[0] == "Circle") {
                     float x = par.at(0).toFloat(), y = par.at(1).toFloat(), radius = par.at(2).toFloat();
                     int r = par.at(3).toInt(&ok,10), g = par.at(4).toInt(&ok,10), b = par.at(5).toInt(&ok,10);
                     std::cout << "Circle("<<x<<","<<y<<","<<radius<<","<<r<<","<<g<<","<<b<<")"<<std::endl;
                     Shape* circle = new Circle(x,y,radius,r,g,b);
                     cont->add(circle);
                 }
                std::getline(data,line);
             }
             shapeContainers.push_back(cont);
             std::cout << "***" << std::endl;
         }
         data.close();
         if (shapeContainers.size()>0) setCurrentContainer(shapeContainers[0]);
     }
}