bool
Preferences::save()
{
	try {
		Glib::RefPtr<Glib::IOChannel> outfile =
			Glib::IOChannel::create_from_file( get_rc_file(), "w");
		outfile->write(keyfile_.to_data());
		outfile->flush();
	}
	catch (const Glib::Error& error) {
		std::cerr << _("Failed to save configuration file.") << std::endl;
		std::cerr << _("An error occurred while attempting to save the file ")
			+ get_rc_file() + _(", this file stores your preferences.");
		std::cerr << std::endl;
		std::cerr << _("The system reported this error as: ") + error.what();
		std::cerr << std::endl;
		return false;
	}
	return true;
}
    void Gui_ResultFileWriter::writeRows(const bool detail,const ResultMap& res_map,const std::vector<int>& idxs){
        Glib::RefPtr< Gio::FileOutputStream > fout = m_out_file->append_to();


        if(detail){
            for (auto i : idxs){
                const Result& res_ref = res_map.getResultAt(i);

                const std::string& path = (res_map.getFileFromIdx(i)->get_path());
                const std::string& comment = res_map.getCommentAt(i);

                if(!res_map.getIsNAAt(i)){
                    for (unsigned int j=0; j<res_ref.size();++j){
                        std::stringstream ss;
                        const OneObjectRow& oor = res_ref.getRow(j);
                        ss <<"\""<<path<<"\", "<< oor.print()<<", \""<<comment<<"\""<<std::endl;
                        fout->write(ss.str());
                    }
                }
            }
        }
        else{
            for (auto i : idxs){
                const Result& res_ref = res_map.getResultAt(i);
                Glib::RefPtr<Gio::File> tmp_file = res_map.getFileFromIdx(i);
                const std::vector<int> roi_keys = res_ref.getROIs();
                const std::string& comment = res_map.getCommentAt(i);

                std::map < unsigned int,std::pair<unsigned int,unsigned int> > table;
                table[0].first = res_ref.getNValid();
                table[0].second =  res_ref.size() - res_ref.getNValid();
                for(unsigned int i=0; i != (unsigned int)res_ref.size(); ++i){
                    OneObjectRow object = res_ref.getRow(i);
                    int roi = object.getROI();
                    if (roi > 0){
                        if(object.getGUIValid() && object.isValid()) //otherwise colour filters etc. don't work
                            ++(table[roi].first);
                        else
                            ++(table[roi].second);
                    }
                }


                for (auto &it : table){
                    int roi = it.first;
                    if ((table.size() == 1) || (roi >= 1)){

                        std::stringstream ss;
                        ss  <<i<<","
                            <<"\""<<tmp_file->get_basename()<<"\""<<","
                            <<roi<<",";

                        if(!res_map.getIsNAAt(i)){
                            ss<<it.second.first<<","
                            <<it.second.second<<",";
                        }
                        else{
                            ss<<"NA,NA,";
                        }
                            ss<<res_ref.getROIClusterData(roi).clusterPop(1)<<","
                            <<res_ref.getROIClusterData(roi).clusterPop(2)<<","
                            <<res_ref.getROIClusterData(roi).clusterPop(3)<<","
                            <<"\""<<comment<<"\""<<","
                            <<"\""<<tmp_file->get_path()<<"\","
                            <<"\""<<res_ref.getROIClusterData(roi).str()<<"\""<<std::endl;

                        fout->write(ss.str());
                    }
                }
            }
        }
        fout->flush ();
        fout->close();

    }
void Gui_ResultFileWriter::writeRows(const bool detail,const ResultMap& res_map,const std::vector<int>& idxs){
    Glib::RefPtr< Gio::FileOutputStream > fout = m_out_file->append_to();

    if(detail){
        for (auto i : idxs){
            const Result& res_ref = res_map.getResultAt(i);

            const std::string& path = (res_map.getFileFromIdx(i)->get_path());
            const std::string& comment = res_map.getCommentAt(i);

            if(!res_map.getIsNAAt(i)){
                for (unsigned int j=0; j<res_ref.size();++j){
                    std::stringstream ss;
                    const OneObjectRow& oor = res_ref.getRow(j);
                    cv::Scalar col = oor.getBGRMean();

                    cv::Point2f center = (oor.getPoint(0) + oor.getPoint(2) ) * 0.5;
                    ss  <<"\""<<path<<"\""<<","
                        <<oor.isValid()<<","
                        <<center.x<<","
                        <<center.y<<","
                        <<oor.getROI()<<","
                        <<(int)oor.getArea()<<","
                        <<oor.getRadius()<<","
                        <<col[2]<<","
                        <<col[1]<<","
                        <<col[0]<<","
                        <<oor.getHue()<<","
                        <<oor.getSat()<<","
                        <<oor.getNInClust()<<","
                        <<"\""<<comment<<"\""<<std::endl;
                    fout->write(ss.str());
                }
            }
        }
    }
    else{
        for (auto i : idxs){
            const Result& res_ref = res_map.getResultAt(i);
            Glib::RefPtr<Gio::File> tmp_file = res_map.getFileFromIdx(i);

            const std::string& comment = res_map.getCommentAt(i);

            std::stringstream ss;
            ss  <<i<<","
                <<"\""<<tmp_file->get_basename()<<"\""<<",";
            if(!res_map.getIsNAAt(i)){
                ss<<res_ref.getNValid()<<","
                <<res_ref.size() - res_ref.getNValid()<<",";
            }
            else{
                ss<<"NA,NA,";
            }
                ss<<comment<<","
                <<"\""<<tmp_file->get_path()<<"\""<<std::endl;

            fout->write(ss.str());
        }
    }
    fout->flush ();
    fout->close();

}