comparison main/MainWindow.cpp @ 1451:e4f223fc9099 svg

Implement "Export SVG file..."
author Chris Cannam
date Mon, 19 Dec 2016 16:32:34 +0000
parents 11484141d969
children 96057b1185cb
comparison
equal deleted inserted replaced
1450:c8b03c666185 1451:e4f223fc9099
584 menu->addSeparator(); 584 menu->addSeparator();
585 585
586 action = new QAction(tr("Export Image File..."), this); 586 action = new QAction(tr("Export Image File..."), this);
587 action->setStatusTip(tr("Export a single pane to an image file")); 587 action->setStatusTip(tr("Export a single pane to an image file"));
588 connect(action, SIGNAL(triggered()), this, SLOT(exportImage())); 588 connect(action, SIGNAL(triggered()), this, SLOT(exportImage()));
589 connect(this, SIGNAL(canExportImage(bool)), action, SLOT(setEnabled(bool)));
590 menu->addAction(action);
591
592 action = new QAction(tr("Export SVG File..."), this);
593 action->setStatusTip(tr("Export a single pane to a scalable SVG image file"));
594 connect(action, SIGNAL(triggered()), this, SLOT(exportSVG()));
589 connect(this, SIGNAL(canExportImage(bool)), action, SLOT(setEnabled(bool))); 595 connect(this, SIGNAL(canExportImage(bool)), action, SLOT(setEnabled(bool)));
590 menu->addAction(action); 596 menu->addAction(action);
591 597
592 menu->addSeparator(); 598 menu->addSeparator();
593 599
2854 MainWindow::exportImage() 2860 MainWindow::exportImage()
2855 { 2861 {
2856 Pane *pane = m_paneStack->getCurrentPane(); 2862 Pane *pane = m_paneStack->getCurrentPane();
2857 if (!pane) return; 2863 if (!pane) return;
2858 2864
2865 //!!! QString path = getSaveFileName(FileFinder::ImageFile);
2866 // if (path == "") return;
2867 // if (QFileInfo(path).suffix() == "") path += ".png";
2859 QString path = getSaveFileName(FileFinder::ImageFile); 2868 QString path = getSaveFileName(FileFinder::ImageFile);
2860
2861 if (path == "") return; 2869 if (path == "") return;
2862 2870 if (QFileInfo(path).suffix() == "") path += ".svg";
2863 if (QFileInfo(path).suffix() == "") path += ".png"; 2871
2864 2872 bool scalable = true; //!!!
2873
2865 bool haveSelection = m_viewManager && !m_viewManager->getSelections().empty(); 2874 bool haveSelection = m_viewManager && !m_viewManager->getSelections().empty();
2866 2875
2867 QSize total, visible, selected; 2876 QSize total, visible, selected;
2868 total = pane->getImageSize(); 2877 total = pane->getRenderedImageSize();
2869 visible = pane->getImageSize(pane->getFirstVisibleFrame(), 2878 visible = pane->getRenderedPartImageSize(pane->getFirstVisibleFrame(),
2870 pane->getLastVisibleFrame()); 2879 pane->getLastVisibleFrame());
2871 2880
2872 sv_frame_t sf0 = 0, sf1 = 0; 2881 sv_frame_t sf0 = 0, sf1 = 0;
2873 2882
2874 if (haveSelection) { 2883 if (haveSelection) {
2875 MultiSelection::SelectionList selections = m_viewManager->getSelections(); 2884 MultiSelection::SelectionList selections = m_viewManager->getSelections();
2876 sf0 = selections.begin()->getStartFrame(); 2885 sf0 = selections.begin()->getStartFrame();
2877 MultiSelection::SelectionList::iterator e = selections.end(); 2886 MultiSelection::SelectionList::iterator e = selections.end();
2878 --e; 2887 --e;
2879 sf1 = e->getEndFrame(); 2888 sf1 = e->getEndFrame();
2880 selected = pane->getImageSize(sf0, sf1); 2889 selected = pane->getRenderedPartImageSize(sf0, sf1);
2881 } 2890 }
2882 2891
2883 QStringList items; 2892 QStringList items;
2884 items << tr("Export the whole pane (%1x%2 pixels)") 2893 items << tr("Export the whole pane (%1x%2 pixels)")
2885 .arg(total.width()).arg(total.height()); 2894 .arg(total.width()).arg(total.height());
2894 2903
2895 QSettings settings; 2904 QSettings settings;
2896 settings.beginGroup("MainWindow"); 2905 settings.beginGroup("MainWindow");
2897 int deflt = settings.value("lastimageexportregion", 0).toInt(); 2906 int deflt = settings.value("lastimageexportregion", 0).toInt();
2898 if (deflt == 2 && !haveSelection) deflt = 1; 2907 if (deflt == 2 && !haveSelection) deflt = 1;
2899 if (deflt == 0 && total.width() > 32767) deflt = 1; 2908 if (deflt == 0 && !scalable && total.width() > 32767) deflt = 1;
2900 2909
2901 ListInputDialog *lid = new ListInputDialog 2910 ListInputDialog *lid = new ListInputDialog
2902 (this, tr("Select region to export"), 2911 (this, tr("Select region to export"),
2903 tr("Which region of the current pane do you want to export as an image?"), 2912 tr("Which region of the current pane do you want to export as an image?"),
2904 items, deflt); 2913 items, deflt);
2905 2914
2906 if (!haveSelection) { 2915 if (!haveSelection) {
2907 lid->setItemAvailability(2, false); 2916 lid->setItemAvailability(2, false);
2908 } 2917 }
2909 if (total.width() > 32767) { // appears to be the limit of a QImage 2918 if (!scalable && total.width() > 32767) { // appears to be limit of a QImage
2910 lid->setItemAvailability(0, false); 2919 lid->setItemAvailability(0, false);
2911 lid->setFootnote(tr("Note: the whole pane is too wide to be exported as a single image.")); 2920 lid->setFootnote(tr("Note: the whole pane is too wide to be exported as a single image."));
2912 } 2921 }
2913 2922
2914 bool ok = lid->exec(); 2923 bool ok = lid->exec();
2917 2926
2918 if (!ok || item.isEmpty()) return; 2927 if (!ok || item.isEmpty()) return;
2919 2928
2920 settings.setValue("lastimageexportregion", deflt); 2929 settings.setValue("lastimageexportregion", deflt);
2921 2930
2922 QImage *image = 0; 2931 if (scalable) {
2923 2932
2933 bool result = false;
2934
2935 if (item == items[0]) {
2936 result = pane->renderToSvgFile(path );
2937 } else if (item == items[1]) {
2938 result = pane->renderPartToSvgFile(path,
2939 pane->getFirstVisibleFrame(),
2940 pane->getLastVisibleFrame());
2941 } else if (haveSelection) {
2942 result = pane->renderPartToSvgFile(path, sf0, sf1);
2943 }
2944
2945 if (!result) {
2946 QMessageBox::critical(this, tr("Failed to save SVG file"),
2947 tr("Failed to save SVG file %1").arg(path));
2948 }
2949
2950 } else {
2951
2952 QImage *image = 0;
2953
2954 if (item == items[0]) {
2955 image = pane->renderToNewImage();
2956 } else if (item == items[1]) {
2957 image = pane->renderPartToNewImage(pane->getFirstVisibleFrame(),
2958 pane->getLastVisibleFrame());
2959 } else if (haveSelection) {
2960 image = pane->renderPartToNewImage(sf0, sf1);
2961 }
2962
2963 if (!image) return;
2964
2965 if (!image->save(path, "PNG")) {
2966 QMessageBox::critical(this, tr("Failed to save image file"),
2967 tr("Failed to save image file %1").arg(path));
2968 }
2969
2970 delete image;
2971 }
2972 }
2973
2974 void
2975 MainWindow::exportSVG()
2976 {
2977 Pane *pane = m_paneStack->getCurrentPane();
2978 if (!pane) return;
2979
2980 QString path = getSaveFileName(FileFinder::SVGFile);
2981 if (path == "") return;
2982 if (QFileInfo(path).suffix() == "") path += ".svg";
2983
2984 bool haveSelection = m_viewManager && !m_viewManager->getSelections().empty();
2985
2986 sv_frame_t sf0 = 0, sf1 = 0;
2987
2988 if (haveSelection) {
2989 MultiSelection::SelectionList selections = m_viewManager->getSelections();
2990 sf0 = selections.begin()->getStartFrame();
2991 MultiSelection::SelectionList::iterator e = selections.end();
2992 --e;
2993 sf1 = e->getEndFrame();
2994 }
2995
2996 QStringList items;
2997 items << tr("Export the whole pane");
2998 items << tr("Export the visible area only");
2999 items << tr("Export the selection extent");
3000
3001 QSettings settings;
3002 settings.beginGroup("MainWindow");
3003 int deflt = settings.value("lastsvgexportregion", 0).toInt();
3004 if (deflt == 2 && !haveSelection) deflt = 1;
3005
3006 ListInputDialog *lid = new ListInputDialog
3007 (this, tr("Select region to export"),
3008 tr("Which region of the current pane do you want to export as a scalable SVG image?"),
3009 items, deflt);
3010
3011 if (!haveSelection) {
3012 lid->setItemAvailability(2, false);
3013 }
3014
3015 bool ok = lid->exec();
3016 QString item = lid->getCurrentString();
3017 delete lid;
3018
3019 if (!ok || item.isEmpty()) return;
3020
3021 settings.setValue("lastsvgexportregion", deflt);
3022
3023 bool result = false;
3024
2924 if (item == items[0]) { 3025 if (item == items[0]) {
2925 image = pane->toNewImage(); 3026 result = pane->renderToSvgFile(path );
2926 } else if (item == items[1]) { 3027 } else if (item == items[1]) {
2927 image = pane->toNewImage(pane->getFirstVisibleFrame(), 3028 result = pane->renderPartToSvgFile(path,
2928 pane->getLastVisibleFrame()); 3029 pane->getFirstVisibleFrame(),
3030 pane->getLastVisibleFrame());
2929 } else if (haveSelection) { 3031 } else if (haveSelection) {
2930 image = pane->toNewImage(sf0, sf1); 3032 result = pane->renderPartToSvgFile(path, sf0, sf1);
2931 }
2932
2933 if (!image) return;
2934
2935 if (!image->save(path, "PNG")) {
2936 QMessageBox::critical(this, tr("Failed to save image file"),
2937 tr("Failed to save image file %1").arg(path));
2938 } 3033 }
2939 3034
2940 delete image; 3035 if (!result) {
3036 QMessageBox::critical(this, tr("Failed to save SVG file"),
3037 tr("Failed to save SVG file %1").arg(path));
3038 }
2941 } 3039 }
2942 3040
2943 void 3041 void
2944 MainWindow::browseRecordedAudio() 3042 MainWindow::browseRecordedAudio()
2945 { 3043 {