From cab03a58299d55ed9a0ae4eb798cda633dfede75 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Tue, 28 Apr 2020 22:41:35 +0200 Subject: [PATCH] Save and load for .lyimg formats in image properties dialog. --- src/img/img/ImagePropertiesPage.ui | 213 +++++++++++++++-------------- src/img/img/imgObject.cc | 38 +++++ src/img/img/imgObject.h | 5 + src/img/img/imgPropertiesPage.cc | 28 ++++ src/img/img/imgPropertiesPage.h | 1 + src/img/unit_tests/imgObject.cc | 8 ++ 6 files changed, 190 insertions(+), 103 deletions(-) diff --git a/src/img/img/ImagePropertiesPage.ui b/src/img/img/ImagePropertiesPage.ui index 306dd99c7..d0453605b 100644 --- a/src/img/img/ImagePropertiesPage.ui +++ b/src/img/img/ImagePropertiesPage.ui @@ -53,6 +53,13 @@ 6 + + + + Browse + + + @@ -77,109 +84,7 @@ - - - - - 1 - 0 - - - - QFrame::Box - - - QFrame::Plain - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - Qt::Horizontal - - - QSizePolicy::Fixed - - - - 10 - 20 - - - - - - - - - 1 - 0 - - - - <Filename> - - - - - - - Qt::Horizontal - - - QSizePolicy::Fixed - - - - 10 - 20 - - - - - - - - - - - Browse - - - - - - - <File info> - - - - - - - QFrame::Plain - - - File - - - - + QFrame::NoFrame @@ -442,6 +347,108 @@ + + + + QFrame::Plain + + + File + + + + + + + + 1 + 0 + + + + QFrame::Box + + + QFrame::Plain + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 10 + 20 + + + + + + + + + 1 + 0 + + + + <Filename> + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 10 + 20 + + + + + + + + + + + Save As ... + + + + + + + <File info> + + + diff --git a/src/img/img/imgObject.cc b/src/img/img/imgObject.cc index aed1b1c4d..c7e55a3ef 100644 --- a/src/img/img/imgObject.cc +++ b/src/img/img/imgObject.cc @@ -23,6 +23,7 @@ #include "imgObject.h" #include "imgWidgets.h" // for interpolate_color() +#include "imgStream.h" #include "tlLog.h" #include "tlTimer.h" #include "layPlugin.h" @@ -1487,6 +1488,24 @@ Object::read_file () tl::info << "Reading image file " << m_filename; } + try { + + tl::InputFile file (m_filename); + tl::InputStream stream (file); + std::auto_ptr read; + read.reset (img::ImageStreamer::read (stream)); + read->m_filename = m_filename; + + // for now we need to copy here ... + *this = *read; + + // exit on success + return; + + } catch (...) { + // continue with other formats ... + } + QImage qimage (tl::to_qstring (m_filename)); if (! qimage.isNull ()) { @@ -1717,6 +1736,25 @@ Object::to_string () const return os.str (); } +void +Object::swap (Object &other) +{ + m_filename.swap (other.m_filename); + std::swap (m_trans, other.m_trans); + std::swap (mp_data, other.mp_data); + std::swap (m_id, other.m_id); + std::swap (m_min_value, other.m_min_value); + std::swap (m_max_value, other.m_max_value); + std::swap (m_min_value_set, other.m_min_value_set); + std::swap (m_max_value_set, other.m_max_value_set); + std::swap (m_data_mapping, other.m_data_mapping); + std::swap (m_visible, other.m_visible); + std::swap (mp_pixel_data, other.mp_pixel_data); + m_landmarks.swap (other.m_landmarks); + std::swap (m_z_position, other.m_z_position); + std::swap (m_updates_enabled, other.m_updates_enabled); +} + size_t Object::width () const { diff --git a/src/img/img/imgObject.h b/src/img/img/imgObject.h index d10348204..b68e5a040 100644 --- a/src/img/img/imgObject.h +++ b/src/img/img/imgObject.h @@ -946,6 +946,11 @@ public: */ virtual std::string to_string () const; + /** + * @brief Swap with another image object + */ + void swap (img::Object &other); + /** * @brief Return the memory used in bytes */ diff --git a/src/img/img/imgPropertiesPage.cc b/src/img/img/imgPropertiesPage.cc index 9a90cc34d..f53c0a100 100644 --- a/src/img/img/imgPropertiesPage.cc +++ b/src/img/img/imgPropertiesPage.cc @@ -23,9 +23,11 @@ #include "imgPropertiesPage.h" #include "imgLandmarksDialog.h" +#include "imgStream.h" #include "layLayoutView.h" #include "layFileDialog.h" #include "tlExceptions.h" +#include "tlFileUtils.h" namespace img { @@ -127,6 +129,7 @@ PropertiesPage::init () connect (value_le, SIGNAL (returnPressed ()), this, SLOT (value_return_pressed ())); connect (reset_pb, SIGNAL (clicked ()), this, SLOT (reset_pressed ())); + connect (save_pb, SIGNAL (clicked ()), this, SLOT (save_pressed ())); connect (preview_cbx, SIGNAL (clicked ()), this, SLOT (preview_checked ())); connect (define_landmarks_pb, SIGNAL (clicked ()), this, SLOT (define_landmarks_pressed ())); } @@ -838,6 +841,31 @@ BEGIN_PROTECTED END_PROTECTED } +void +PropertiesPage::save_pressed () +{ +BEGIN_PROTECTED + + apply (); + + lay::FileDialog file_dialog (this, tl::to_string (QObject::tr ("Save As KLayout Image File")), tl::to_string (QObject::tr ("KLayout image files (*.lyimg);;All files (*)"))); + + std::string filename = mp_direct_image->filename (); + if (! filename.empty () && tl::extension (filename) != "lyimg") { + filename = tl::basename (filename) + ".lyimg"; + } + + if (file_dialog.get_save (filename)) { + + tl::OutputFile file (filename); + tl::OutputStream stream (file); + img::ImageStreamer::write (stream, *mp_direct_image); + + } + +END_PROTECTED +} + void PropertiesPage::reset_pressed () { diff --git a/src/img/img/imgPropertiesPage.h b/src/img/img/imgPropertiesPage.h index 6bbdc722d..1de6a3f88 100644 --- a/src/img/img/imgPropertiesPage.h +++ b/src/img/img/imgPropertiesPage.h @@ -86,6 +86,7 @@ private slots: void min_max_return_pressed (); void preview_checked (); void reset_pressed (); + void save_pressed (); void define_landmarks_pressed (); private: diff --git a/src/img/unit_tests/imgObject.cc b/src/img/unit_tests/imgObject.cc index 4cfa10e75..86f6f1b47 100644 --- a/src/img/unit_tests/imgObject.cc +++ b/src/img/unit_tests/imgObject.cc @@ -130,6 +130,14 @@ TEST(1) EXPECT_EQ (copy2.data_mapping ().false_color_nodes.size (), size_t (4)); EXPECT_EQ (copy2.equals (&image), true); + img::Object copy3, empty; + copy3.swap (copy2); + EXPECT_EQ (copy3.equals (&image), true); + EXPECT_EQ (copy2.equals (&empty), true); + copy3.swap (copy2); + EXPECT_EQ (copy2.equals (&image), true); + EXPECT_EQ (copy3.equals (&empty), true); + EXPECT_EQ (image.to_string (), copy2.to_string ()); EXPECT_EQ (image.mask (1, 2), true);