view Framework/Widgets/WidgetBase.cpp @ 53:c2dc924f1a63 wasm

removing threading out of the framework
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 27 Apr 2017 16:57:49 +0200
parents 25befef48c35
children ca644004d2ee
line wrap: on
line source

/**
 * Stone of Orthanc
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital of Liege, Belgium
 * Copyright (C) 2017 Osimis, Belgium
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/


#include "WidgetBase.h"

#include "../../Resources/Orthanc/Core/OrthancException.h"
#include "../../Resources/Orthanc/Core/Images/ImageProcessing.h"
#include "../../Resources/Orthanc/Core/Logging.h"

namespace OrthancStone
{
  void WidgetBase::ClearBackgroundOrthanc(Orthanc::ImageAccessor& target) const 
  {
    // Clear the background using Orthanc

    if (backgroundCleared_)
    {
      Orthanc::ImageProcessing::Set(target, 
                                    backgroundColor_[0],
                                    backgroundColor_[1],
                                    backgroundColor_[2],
                                    255 /* alpha */);
    }
  }


  void WidgetBase::ClearBackgroundCairo(CairoContext& context) const
  {
    // Clear the background using Cairo

    if (IsBackgroundCleared())
    {
      uint8_t red, green, blue;
      GetBackgroundColor(red, green, blue);

      context.SetSourceColor(red, green, blue);
      cairo_paint(context.GetObject());
    }
  }


  void WidgetBase::ClearBackgroundCairo(Orthanc::ImageAccessor& target) const
  {
    CairoSurface surface(target);
    CairoContext context(surface);
    ClearBackgroundCairo(context);
  }


  void WidgetBase::NotifyChange()
  {
    observers_.NotifyChange(this);
  }


  void WidgetBase::UpdateStatusBar(const std::string& message)
  {
    if (statusBar_ != NULL)
    {
      statusBar_->SetMessage(message);
    }
  }


  WidgetBase::WidgetBase() :
    statusBar_(NULL),
    started_(false),
    backgroundCleared_(false)
  {
    backgroundColor_[0] = 0;
    backgroundColor_[1] = 0;
    backgroundColor_[2] = 0;
  }


  void WidgetBase::SetBackgroundColor(uint8_t red,
                                      uint8_t green,
                                      uint8_t blue)
  {
    backgroundColor_[0] = red;
    backgroundColor_[1] = green;
    backgroundColor_[2] = blue;
  }

  void WidgetBase::GetBackgroundColor(uint8_t& red,
                                      uint8_t& green,
                                      uint8_t& blue) const
  {
    red = backgroundColor_[0];
    green = backgroundColor_[1];
    blue = backgroundColor_[2];
  }


  void WidgetBase::Register(IChangeObserver& observer)
  {
    if (started_)
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
    }

    observers_.Register(observer);
  }


  void WidgetBase::Unregister(IChangeObserver& observer)
  {
    if (started_)
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
    }

    observers_.Unregister(observer);
  }


  void WidgetBase::Start()
  {
    if (started_)
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
    }
    else
    {
      started_ = true;
    }
  }

  
  void WidgetBase::Stop()
  {
    if (started_)
    {
      started_ = false;
    }
    else
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
    }
  }

  
  bool WidgetBase::Render(Orthanc::ImageAccessor& surface)
  {
    if (!started_)
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
    }
    
#if 0
    ClearBackgroundOrthanc(surface);
#else
    ClearBackgroundCairo(surface);  // Faster than Orthanc
#endif

    return true;
  }

  
  void WidgetBase::UpdateContent()
  {
    throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
  }
}